I have function when executed will plot routes on a map from an array of values. I am using Google maps *Directions Render*. From research i have seen, to render multiple routes on a map i need to create a Directions Render for each route in the array.
I am trying to create a Directions Render for each route in the array so i have decided to concat the name of the of the variabe with the index 'i'. this does not work.
I created the Directions Render object as an array however when accessing the array to pot the points i am getting an error - TypeError: can't convert undefined to object directionsService[i] = new google.maps.DirectionsService();
If i create separate Directions Render e.g. DirectionsRender0,DirectionsRender1, DirectionsRender2... this works fine however there will be an instance where i am not sure how many Directions Render there will be.
Under is an example of my code:
function plotRoutes(){
var directionsDisplay = new Array();
for (var i=0; i< startLoc.length; i++){
var rendererOptions = {
map: map,
preserveViewport:true
}
directionsService[i] = new google.maps.DirectionsService();
var travelMode = google.maps.DirectionsTravelMode.DRIVING;
var request = {
origin: startLoc[i],
destination: endLoc[i],
travelMode: travelMode
};
directionsDisplay[i] = new google.maps.DirectionsRenderer(rendererOptions);
directionsDisplay[i].setMap(map);
directionsService.route(request, function(response, status) {
//console.log(response);
if (status == google.maps.DirectionsStatus.OK){
console.log(response);
directionsDisplay[i].setDirections(response);
}
});
}