我有这样的要求
Source,
waypoint 1,
waypoint 2,
waypoint 3,
Destination
我需要Source
到waypoint 1
的路径是一种颜色,waypoint 1
到waypoint 2
的路径是另一种颜色。
这里我得到整个路径显示在单一(红色)颜色。但是当我们移动到下一个路径点时我需要改变不同的颜色
mapSite.getDirection = function(latitudeValue, longitudeValue, waypts) {
document.getElementById('directionsPanel').innerHTML = "";
directionsDisplay = new google.maps.DirectionsRenderer({
polylineOptions: {
strokeColor: "red"
}
});
var site = new google.maps.LatLng(latitudeValue, longitudeValue);
var mapOptions = {
center: site,
zoom: 9,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map'), mapOptions);
mapSite.map = map;
directionsDisplay.setMap(mapSite.map);
var start = mapSite.latitude + "," + mapSite.longitude;
var end = latitudeValue + "," + longitudeValue;
var request = {
origin: start,
destination: end,
waypoints: waypts,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
directionsDisplay.setPanel(document.getElementById('directionsPanel'));
}
});
};
我认为最简单的方法是多次调用方向api。例如,源->路点1,路点1->路点2,路点2->路点3,路点3->目的地…
否则,您从回调函数directionsService.route(request, function(response, status) {
获得的response
,包括legs
和steps
以及polyline。您可以自己绘制路由,而不是调用directionsDisplay.setDirections(response);
请参阅官方文档以获取更多响应格式的信息。