我试图在同一张地图上显示多条路线,但无法这样做。
无论我做什么,我都只能得到一条路线。
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
任何指示都会有所帮助。
我有同样的问题。这个线程 https://groups.google.com/forum/?fromgroups=#!topic/google-maps-js-api-v3/ubomY2IT728谷歌地图的谷歌小组展示了如何做到这一点。
作者(谷歌员工)写道:
您应该能够创建两个 DirectionsRenderer 对象,每个对象
使用相同的地图和不同的 DirectionsResults。
var map = new google.maps.Map(document.getElementById("map_canvas"));
function renderDirections(result) {
var directionsRenderer = new google.maps.DirectionsRenderer;
directionsRenderer.setMap(map);
directionsRenderer.setDirections(result);
}
var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result) {
renderDirections(result);
});
}
requestDirections('Huntsville, AL', 'Boston, MA');
requestDirections('Bakersfield, CA', 'Vancouver, BC');
我尝试了一下,确实有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)