I would like to draw multiple routes based on the directions service in Google, the code goes below
p/s:Data is a list I obtained from my json call
for (i = 0; i < 20; i++) {
route = data[i];
start = new google.maps.LatLng(route.from_lat,route.from_lng);
end = new google.maps.LatLng(route.to_lat,route.to_lng);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
var rendererOptions = {
preserveViewport: true,
suppressMarkers:true,
routeIndex:i
};
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay.setMap(map);
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(result, status) {
console.log(result);
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}});
I can't seems to find much relevant information from forums, is my best bet to obtain all the individual traffic information and draw again using polyline? And also, how do I make sure the loop carries on only when my directionDisplay.setDirections is done?
Regards, Andy
Alternate routes appear as gray lines on the map. Tap one of the gray lines to get directions. You can further customize your route on Google Maps by tapping and dragging along the blue line.
You need separate google.maps.DirectionsRenderer object for each route. Something like that:
var routes = [{origin: ..., destination: ...}, {origin: ..., destination: ...}, {origin: ..., destination: ...}];
var rendererOptions = {
preserveViewport: true,
suppressMarkers:true,
routeIndex:i
};
var directionsService = new google.maps.DirectionsService();
routes.each(function(route){
var request = {
origin: route.origin,
destination: route.destination,
travelMode: google.maps.TravelMode.DRIVING
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay.setMap(map);
directionsService.route(request, function(result, status) {
console.log(result);
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
});
I'm using prototype.js, so your method of array walking may differ.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With