我想將多個Google地圖路線輸出到一個Google地圖上。數據來自具有多個標記的XML文件。每個標記都有一個起點和一個終點(以緯度和經度值的形式)。然後將這些標記添加到地圖中,並調用Google Maps Directions Service來繪製每個標記的起點和終點之間的路線。在一張地圖上顯示多個Google地圖標記和路線
我遇到的問題是隻繪製一條路線(如果我刷新頁面,它似乎只是隨機選取其中一個標記並繪製這兩個標記之間的方向)。其他標記完全不顯示。
我已經console.logged for循環來檢查它是否運行在XML(這是)每個標記。我猜這是什麼做這裏這兩條線,但是這是一個猜測...
directionsDisplay = new google.maps.DirectionsRenderer();
directionsService = new google.maps.DirectionsService();
我讀過這個問題 - Using Google Maps 3 API to get multiple routes on a Map,但真的不理解答案/知道如何與我的情況有關。謝謝。
jQuery(document).ready(function($) {
var map = new google.maps.Map(document.getElementById("map_canvas"), {
zoom: 13,
mapTypeId: 'roadmap'
});
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(pos);
map.setZoom(13);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
directionsDisplay = new google.maps.DirectionsRenderer();
directionsService = new google.maps.DirectionsService();
// Change this depending on the name of your PHP file
downloadUrl("xml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var title = markers[i].getAttribute("title");
mode = markers[i].getAttribute("mode");
startpoint = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("startlat")),
parseFloat(markers[i].getAttribute("startlng")));
endpoint = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("endlat")),
parseFloat(markers[i].getAttribute("endlng")));
calcRoute();
console.log('marker');
}
});
directionsDisplay.setMap(map);
});
function calcRoute() {
var request = {
origin: startpoint,
destination: endpoint,
//waypoints: waypts,
travelMode: google.maps.DirectionsTravelMode[mode]
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
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);
}
function doNothing() {}
謝謝你的答案邁克爾。我已經嘗試了你的代碼,但得到了'未捕獲的TypeError:無法在此行讀取'undefined'屬性'documentElement' - var markers = xml.documentElement.getElementsByTagName(「marker」);有任何想法嗎? – GuerillaRadio 2013-04-27 18:44:32
對不起,我的糟糕,我忘了仔細檢查代碼,當我改變它使用'$ .get()'而不是'downloadUrl()'時。我更新了代碼以移除'var xml = data.responseXML;',因爲這已不再需要,並且在'$ .get()'調用中添加一個顯式的''xml'數據類型(最後一個參數,在回調之後)。如果你的服務器爲XML文件設置了正確的MIME類型頭信息,那麼這可能不是必須的,但是添加該參數可以確保jQuery爲你解析XML。 – 2013-04-27 19:39:53
非常完美,非常感謝Michael。 – GuerillaRadio 2013-04-27 19:51:08