There are some issues not related to the question:
- you create a new Map-instance on each iteration
- you didn't set the map-property of the Renderer
- you've set the routeIndex to 1, but you get only 1 route(with the index 0)
The issue related to the question: The attempt was good, but the implementation wasn't.
You must create a new DirectionsRenderer-instance for each request(you've done it), but this instance is visible in the function-scope, so the variable will be overwritten on each iteration and when the response arrives each response will use the same DirectionsRenderer-instance(which may only render a single route).
Use a anonymous function to create the DirectionsRenderer-instances and requests:
$.each(routes,
function(i,obj){//<--anonymous function
var request = {
origin: obj.origin,
destination: obj.destination,
travelMode: google.maps.TravelMode.DRIVING
},
//directionsDisplay is only visible in the scope of this anonymous function
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
});});