geoxml3中的文档名称

时间:2014-03-06 12:10:46

标签: geoxml3

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

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

1 个答案:

答案 0 :(得分:2)

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

pmParseFn

传递对xml DOM的引用以获取其关联的地标。 example using it

它并非真正为您的目的而设计,但如果您的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);

working example

working example with 2 different KML files