【发布时间】:2018-04-04 09:11:18
【问题描述】:
使用 Google Chorme(最新版本)和 Sublime Text。
您好,我对我的个人项目感到震惊,我想将(公共汽车的)xml 坐标转换为谷歌地图上的标记,这是我的代码:
<script type="text/javascript">
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
initMap(this);
}
};
xhttp.open("GET", "https://applications002.brest-metropole.fr/WIPOD01/Transport/REST/getGeolocatedVehiclesPosition?format=xml&route_id=1&trip_headsign=Fort%20Montbarey", true);
xhttp.send();
function initMap(xml){
var xlat, xlon, inc, xmlDoc, GeoBus, marker;
xmlDoc = xml.responseXML; //line 48
xlat = xmlDoc.getElementsByTagName('Lat');
xlon = xmlDoc.getElementsByTagName('Lon');
for (inc = 0; inc < xlat.length; inc++) {
var latcode = xlat[inc].childNodes[0].nodeValue;
var loncode = xlon[inc].childNodes[0].nodeValue;
var GeoBus = {lat: latcode, lng: loncode};
new google.maps.Marker({position: GeoBus, map: map});
}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat:48.3908449361675, lng:-4.48680591961381}
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap">
</script>
但我得到了这个问题:
Ligne1.html:48 Uncaught TypeError: Cannot read property 'responseXML' of undefined
at initMap (Ligne1.html:48)
at js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:97
at js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:66
at Object._.Vd (js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:64)
at fe (js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:66)
at js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:147
at Object.google.maps.Load (js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:21)
at js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:146
at js?key=AIzaSyDw8GYeGNJySG_KDkhBFj4uHadD6G1Ahy4&callback=initMap:147
感谢您的帮助!
【问题讨论】:
-
看到这个
https://maps.googleapis.com/maps/api/js?key=...&callback=initMap...它在没有参数的情况下调用initMap...您似乎试图用一个只希望以一种方式调用的函数做两件事 -
感谢我们删除了“&callback=initMap”,但现在我遇到了这个错误:InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number
-
xlat[inc].childNodes[0].nodeValue可能不是一个数字 -console.log(JSON.stringify(GeoBus))分配给它之后...看起来对吗? -
不,它是一个 字符串 - 请参阅下面的答案
标签: javascript xml google-maps google-maps-api-3 google-maps-markers