我想将多条 Google map 路线输出到一张 Google map 上。数据取自具有多个标记的 XML 文件。每个标记都有一个起点和一个终点(以纬度和经度值的形式)。然后将这些标记添加到 map 中,并调用 Google map 路线服务以在每个标记的起点和终点之间绘制一条路线。
我遇到的问题是只绘制了一条路线(如果我刷新页面,它似乎只是随机选择一个标记并绘制这两个标记之间的方向)。其他标记根本不显示。
我已经通过 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() {}
最佳答案
我认为您链接到的答案中的关键点是每条路线都需要自己的 DirectionsRenderer
对象。它没有提到它,但似乎每条路线也应该有自己的 DirectionsService
对象。您在所有 route 共享这些对象中的每一个。我怀疑这些共享对象在每条路线上都被覆盖了。
这是一个应该通过为每条路线创建新对象来解决这个问题的更新:
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);
}
// Change this depending on the name of your PHP file
$.get("xml.php", function( xml ) {
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
addMarkerPair( markers[i] );
}
}, 'xml' );
});
function addMarkerPair( pair ) {
function get( name ) {
return pair.getAttribute( name );
}
var title = get("title");
var mode = get("mode");
var startpoint = new google.maps.LatLng(
+get("startlat"),
+get("startlng")
);
var endpoint = new google.maps.LatLng(
+get("endlat"),
+get("endlng")
);
calcRoute( mode, startpoint, endpoint );
console.log('marker');
}
function calcRoute( mode, startpoint, endpoint ) {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
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);
}
});
}
我还冒昧地修复和简化了一些事情:
- 将标记循环主体移出到单独的
addMarkerPair
函数中以提高可读性。 - 在
addMarkerPair
中添加了一个get
函数作为pair.getAttribute
的简写。 - 使用
+
而不是parseInt
。任何一个都会做同样的事情;我喜欢+
的简洁性。 - 添加几个缺失的
var
声明。 - 将必要的参数作为函数参数而不是全局变量传递给
calcRoute
。 - 使用
$.get
而不是downloadUrl
。当 jQuery 已有函数时,无需您自己的函数。
此外,作为 XML 解析的另一种替代方法,您可以使用 jQuery 来访问 XML 元素。但我坚持使用更接近您已经在做的方式的代码。
关于javascript - 在一张 map 上显示多个谷歌地图标记和方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16254867/