我想在地图上显示几条路线,但我更愿意先用谷歌地图绘制它们。例如,我得到了从西雅图到圣地亚哥的路线,然后移动了一些东西,链接看起来像this。
我知道我可以使用DirectionsRenderer绘制连接西雅图和圣地亚哥的折线,如下所示:
function renderDirections(result) {
var directionsRenderer = new google.maps.DirectionsRenderer;
directionsRenderer.setMap(gMap);
directionsRenderer.setDirections(result);
}
var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.BICYCLING
}, function(result) {
renderDirections(result);
});
}
requestDirections('Seattle, WA', 'San Diego, CA');
我想知道的是,如果有方法可以按照路线请求传递链接。该链接包含航点,我对默认路线的修改。
答案 0 :(得分:0)
是的,只要您将起点和终点传递到DirectionsRequest,并将其传递到DirectionsService对象,就可以使用DirectionsRenderer。一旦你调用.setDirections,它就会为你绘制折线。来自API文档at。
设置渲染器以使用DirectionsService中的结果。以这种方式设置一组有效的路线将在渲染器的指定地图和面板上显示路线。
如果你得到的是自己绘制折线(虽然我不明白为什么它是必要的),路径中的各个点可以派生出来 - DirectionsResult包含一个DirectionsLegs数组,其中包含一个数组DirectionsSteps包含.path属性,这是一个latlng数组。 (噢!)
答案 1 :(得分:0)