这是我的代码。
我从xml获取数据。
但它不起作用..
请看哪里有错误。
我找不到它..我正在研究它超过一个星期......
但没有:(
P.S。对不起我的英文..
这是我的XML数据..
<marker>
<line id_line="1" colour="#ccc" width="4" users="line" coordinats="47.828979,40.974844;47.839451,40.985211;47.848377,40.981064;47.853699,40.984822">
<point lng="40.974844" lat="47.828979"/>
<point lng="40.985211" lat="47.839451"/>
<point lng="40.981064" lat="47.848377"/>
<point lng="40.984822" lat="47.853699"/>
</line>
</marker>
JavaScript的:
function line() {
var point=[];
downloadUrl("line.php", function(data) {
var xml = data.responseXML;
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
var lines = xmlDoc.documentElement.getElementsByTagName("line");
for (var a = 0; a < lines.length; a++) {
var colour = lines[a].getAttribute("colour");
var width = parseFloat(lines[a].getAttribute("width"));
var points = lines[a].getElementsByTagName("point");
alert (points[i].getAttribute("lat"));
var pts = [];
for (var i = 0; i < points.length; i++) {
pts[i] = new google.maps.LatLng(parseFloat(points[i].getAttribute("lat")),
parseFloat(points[i].getAttribute("lng")));
}
new google.maps.Polyline(pts,colour,width);
pts.setMap(map);
}
google.maps.event.addListener(pointpath,'mouseover', function() {
this.setOptions({strokeColor: '#3ADF00' });
this.setOptions({strokeOpacity: 1.0 });
this.setOptions({strokeWeight: 4 });
});
google.maps.event.addListener(pointpath,'mouseout', function() {
this.setOptions({strokeColor: '#FF0040' });
this.setOptions({strokeOpacity: 1.0 });
this.setOptions({strokeWeight: 2 });
});
var mpenc = new google.maps.InfoWindow();
var contentString = "Status: " + status ;
google.maps.event.addListener(pointpath,'click', function(event) {
mpenc.setContent(contentString);
mpenc.setPosition(event.latLng);
mpenc.open(map);
});
}
});
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
}
答案 0 :(得分:1)
xmlDoc应该是什么?
var xml = data.responseXML;
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
var lines = xmlDoc.documentElement.getElementsByTagName("line");
看起来像这一行:
var xml = data.responseXML;
应该是:
var xmlDoc = data.responseXML;
如果这没有帮助,请参阅this tutorial in the Google Maps API v3 documentation,“使用PHP输出XML”部分以及有关检查XML输出是否有效的部分。
确保你有这一行:
header("Content-type: text/xml");
标题包含正确的内容类型。在浏览器中打开XML和/或通过验证器运行XML(如果XML无效,requestXML将为null)。