KMLin custom php map

Home Forums MapPress Support KMLin custom php map


Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #10916

    How can I display a KML file in a custom map. I can add a KML to a post and see it in a shortcode  map, but I don’t know how to access that in my custom map. I’m using a custom map in order to display different icons for each poi based on a custom field.



    Hi, assuming you’ve already seen the example PHP code in the docs, you just need to set two things for the POI to display KML:

    1) $poi->type = “kml”

    2) $poi->kml = http://mykmlurl.kml


    For example:

    $mypoi= new Mappress_Poi(array("iconid" => "green-dot", "title" => "KML Example", "type" => "kml", "kml" => ""));

    I have gotten a custom map working and the type value seemed like the piece I was missing, but I still haven’t gotten it to work. I’ve put this example together with the example in the documentation and still no luck.

    This is what I’m testing right now:


    $mymap = new Mappress_Map(array(“width” => “100%”));
    $mypoi_1= new Mappress_Poi(array(“iconid” => “green-dot”, “title” => “KML Example”, “type” => “kml”, “kml” => “”));
    $mypoi_2 = new Mappress_Poi(array(“title” => “500 Chestnut St”, “body” => “Independence National Park, Philadelphia, PA<br/>19106”, “point” => array(“lat” => 39.948712,”lng” => -75.15001)));
    $mymap->pois = array($mypoi_1, $mypoi_2);
    echo $mymap->display(array(“directions”=>”none”));


    It shows the point, but not the kml file. The kml file does display in the shortcode mashup.

    By the way, how do you format code in a post?


    Hi, to format code click the kitchen sink icon on the right of the editor toolbar, then change ‘paragraph’ format to ‘preformatted’.

    Sorry, I should’ve tested the code first.  There are two problems:

    1 – I gave you the wrong syntax.  The KML parameter should itself be an array with ‘url’ as an element (I thought I might eventually add more KML options).  Corrected code is below.

    2-  when a KML is added programmatically (vs. in the Map Editor)  it doesn’t have a center until the file is loaded.  I hadn’t considered that case and the map ends up centering on NULL, which gives a blank map.

    I can easily update the code to use (0,0) as the center if the KML isn’t loaded – but then the map won’t automatically center correctly.

    A better solution is to load the POIs asynchronously, but that may require a lot of re-coding.  Do you plan to set a map center explicitly, or did you need it to automatically center using the KML data?

    If you like, send me a note using the contact form and I can send you the 0,0 fix to try out right away.  Otherwise, I’ll work on the other solution but I can’t say yet when it will be ready.


    $mymap = new Mappress_Map(array("width" => "100%")); 
    $mypoi_1= new Mappress_Poi(array("iconid" => "green-dot", "title" => "KML Example", "type" => "kml", "kml" => array("url" => ""))); 
    $mypoi_2 = new Mappress_Poi(array("title" => "500 Chestnut St", "body" => "Independence National Park, Philadelphia, PA<br/>19106", "point" => array("lat" => 39.948712,"lng" => -75.15001))); 
    $mymap->pois = array($mypoi_1, $mypoi_2); 
    echo $mymap->display(array("directions"=>"none"));


Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.