0

我为我的地图点创建了一个自定义 XML 结构。该结构类似于下面的代码。我想读取这些点并将它们相应地放在地图上,点击时会弹出一个窗口和一个特定的标记图标。我将不胜感激任何帮助。

地图点.xml

<MapPoints>
<MapPoint PointID="1">
    <LocationName></LocationName>
    <LocationAddress></LocationAddress>
    <LocationURL></LocationURL>
    <LocationExt></LocationExt>
    <LocationFax></LocationFax>
    <LocationLat></LocationLat>
    <LocationLong></LocationLong>
    <LocationMarker></LocationMarker>
</MapPoint>
<MapPoint PointID="2">
    <LocationName></LocationName>
    <LocationAddress></LocationAddress>
    <LocationURL></LocationURL>
    <LocationExt></LocationExt>
    <LocationFax></LocationFax>
    <LocationLat></LocationLat>
    <LocationLong></LocationLong>
    <LocationMarker></LocationMarker>
</MapPoint>
<MapPoint PointID="3">
    <LocationName></LocationName>
    <LocationAddress></LocationAddress>
    <LocationURL></LocationURL>
    <LocationExt></LocationExt>
    <LocationFax></LocationFax>
    <LocationLat></LocationLat>
    <LocationLong></LocationLong>
    <LocationMarker></LocationMarker>
</MapPoint>
<MapPoint PointID="4">
    <LocationName></LocationName>
    <LocationAddress></LocationAddress>
    <LocationURL></LocationURL>
    <LocationExt></LocationExt>
    <LocationFax></LocationFax>
    <LocationLat></LocationLat>
    <LocationLong></LocationLong>
    <LocationMarker></LocationMarker>
</MapPoint>
</MapPoints>
4

2 回答 2

0

使用jQuery。在您的 html 文档的开头,放入以下行:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

现在对本地 XML 文件使用 jQuery ajax 调用。

        //pulls in the xml
        $.ajax({
        type: "GET",
            url: "MapPoints.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('MapPoint').each(function(){
                    var lat = $.trim(this.LocationLat);
                    var lng = $.trim(this.LocationLng);

                    //use the same method to extract your other data
                    var mappoint = new google.maps.LatLng(lng,lat);

                    //now create the marker and set it to your map
                    var marker = new google.maps.Marker({
                     position:mappoint,
                     map:map,
                     title:'Your Marker Title',
                     icon:null
                    });

                });

            }
            });
于 2012-08-15T17:36:16.373 回答
0

有很多使用属性以“Mike Williams Google Maps API v2 教程”格式解析 xml 的示例,这里是一个.

要使用您的“自定义”格式,您需要替换此行(查找“MapPoint”而不是“标记”):

var markers = xmlDoc.documentElement.getElementsByTagName("marker");

这些行:

      var lat = parseFloat(markers[i].getAttribute("lat"));
      var lng = parseFloat(markers[i].getAttribute("lng"));
      var point = new google.maps.LatLng(lat,lng);
      var html = markers[i].getAttribute("html");
      var label = markers[i].getAttribute("label");
      // create the marker
      var marker = createMarker(point,label,html);

使用从 xml 中解析元素内容的代码。

要获取元素内容,您需要执行以下操作:

var lat = parseFloat(nodeValue(markers[i].getElementsByTagName("LocationLat")[0]));

其中 nodeValue (从 geoxml3 借来的)是:

//nodeValue: Extract the text value of a DOM node, with leading and trailing whitespace trimmed
geoXML3.nodeValue = function(node) {
  var retStr="";
  if (!node) {
    return '';
  }
   if(node.nodeType==3||node.nodeType==4||node.nodeType==2){
      retStr+=node.nodeValue;
   }else if(node.nodeType==1||node.nodeType==9||node.nodeType==11){
      for(var i=0;i<node.childNodes.length;++i){
         retStr+=arguments.callee(node.childNodes[i]);
      }
   }
   return retStr;
};
于 2012-08-15T15:55:10.210 回答