How would I use Google Maps API to plot a route? E.g to have a bunch of way points loaded onto the map (I currently have this) and draw a line from each of them showing the user a route they could take to see all of them? How would I then load this up when the user see’s the map?
Share
You can set the waypoints property on a DirectionsService object and it will plot the route from the source to the destination via all the points in your array:
Once you have set the waypoints property, you call the route method to calculate the directions:
Once you have your DirectionsResult, you can use the DirectionsRenderer object to render the results on a Google Map.
Update with working example
The following code makes a direction request between hardcoded start and end points via an array of three waypoints:
You can find a working example of this code here (source).
N.B. Keep in mind you can only use up to eight waypoints in your array, unless you switch to a business account.