我为Map Points创建了一个自定义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>
答案 0 :(得分:0)
有许多示例使用属性here is one在“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;
};
答案 1 :(得分: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
});
});
}
});