Google maps api 3 用XML填充Google地图

Google maps api 3 用XML填充Google地图,google-maps-api-3,Google Maps Api 3,我为我的地图点创建了一个自定义XML结构。结构类似于下面的代码。我想读点,并把他们在地图上相应的弹出窗口时,点击和一个特定的标记图标。我将感谢任何帮助 MapPoints.xml <MapPoints> <MapPoint PointID="1"> <LocationName></LocationName> <LocationAddress></LocationAddress> <Locatio

我为我的地图点创建了一个自定义XML结构。结构类似于下面的代码。我想读点,并把他们在地图上相应的弹出窗口时,点击和一个特定的标记图标。我将感谢任何帮助

MapPoints.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>

在“Mike Williams Google Maps API v2教程”格式中,有许多使用属性解析xml的示例

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

以下几行:

      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:提取DOM节点的文本值,并修剪前导和尾随空格
geoXML3.nodeValue=函数(节点){
var retStr=“”;
如果(!节点){
返回“”;
}
if(node.nodeType==3 | | node.nodeType==4 | | node.nodeType==2){
retStr+=node.nodeValue;
}else if(node.nodeType==1 | | node.nodeType==9 | | node.nodeType==11){
对于(var i=0;i使用。在html文档的头部,放置以下行:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
//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;
};
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        //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
                    });

                });

            }
            });