Question

My kml is as under

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
        <name>Car Parks</name>
        <description>Car Parks</description>
        <Folder>
            <name>Data Objects</name>
            <open>1</open>
            <description>data objects</description>
            <Placemark id="CP11">
                <name>CP11</name>
                <description>CP11</description>
                <styleUrl>#0-normal</styleUrl>      
                <Point>
                    <coordinates>4.878205,52.371968,0</coordinates>
                </Point>
            </Placemark>
        </Folder>
    </Document>
</kml>

My javascript is as under

geoXml = new geoXML3.parser({
                createMarker: createMarker
                        }); 

createMarker:function(placemark, doc) {

    var markerOptions = {
      optimized: false
    };

    // Create the marker on the map
    var marker = new google.maps.Marker(markerOptions);
    if (!doc) {
        doc.markers.push(marker);
    }

    google.maps.event.addListener(marker, 'click', function() 
    {            
         // I want to access the document name here of 'car Parks'
         alert(doc.Document);
    });
  }

How can I get document name in marker click event of the marker? Basically when I click the marker on the map, I want to know the type of marker the user has clicked on.

Was it helpful?

Solution

There is an optional placemark parse function

pmParseFn

Which is passed a reference to the xml DOM for its associated placemark. example using it

It isn't really designed for your purpose, but if your KML format is fixed, you can get the <name> of the <Document> tag by doing this:

var map;
var geoXml = null;

function initialize() {
    var latlong = new google.maps.LatLng(59.32, 13.48);

    var googlemaps_options = {
        zoom: 18,
        center: latlong,
        mapTypeId: google.maps.MapTypeId.SATELLITE,
        streetViewControl: false
    }

    map = new google.maps.Map(document.getElementById('map_canvas'), googlemaps_options);
    geoXml = new geoXML3.parser({
               createMarker: createMarker,
               pmParseFn: parsePlacemark,
           map:map
               }); 

  geoXml.parse("http://www.geocodezip.com/geoxml3_test/SO_20140306_name.kml");
}

// Custom placemark parse function
function parsePlacemark (node, placemark) {
      var addressNodes = node.parentNode.parentNode.getElementsByTagName('name');
      var address = null;
      if (addressNodes && addressNodes.length && (addressNodes.length > 0)) {
        placemark.docName = geoXML3.nodeValue(addressNodes[0]);
      }
}

  function createMarker(placemark, doc) {
    var markerOptions = {
      optimized: false,
      position: placemark.latlng,
      map: map
    };

    // Create the marker on the map
    var marker = new google.maps.Marker(markerOptions);
    if (!doc) {
        doc.markers.push(marker);
    }

    google.maps.event.addListener(marker, 'click', function() 
    {            
         // I want to access the document name here of 'car Parks'
         alert(placemark.docName);
    });
  }      
google.maps.event.addDomListener(window, 'load', initialize);

working example

working example with 2 different KML files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top