0

我的kml如下

<?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>

我的javascript如下

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);
    });
  }

如何在标记的标记单击事件中获取文档名称?基本上,当我单击地图上的标记时,我想知道用户单击的标记类型。

4

1 回答 1

2

有一个可选的地标解析功能

pmParseFn

它传递了对其关联地标的 xml DOM 的引用。 使用它的例子

它并不是真正为您的目的而设计的,但如果您的 KML 格式是固定的,您可以通过执行以下操作获取<name>标签<Document>的 :

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);

工作示例

具有 2 个不同 KML 文件的工作示例

于 2014-03-06T15:13:05.123 回答