I am trying to develop an application using MKMapView which needs to plot shortest path.Following is the scenario. I have 5 to 10 coordinates,which are plotted on the map.I have to draw the shortest path to cover all these locations from my current location.This is a travel app,so need to show optimum distance .Please share your thoughts.I had a thought of finding distance to each point from current location but this needs to send multiple request at a time ,which seems to be very hectic process because the point can be 10 to 100. Is there any solution for this available in Google Maps SDK? Looking forward for great ideas and suggestions. Thanks in advance...
-
https://developers.google.com/maps/documentation/directions/intro. check direction api of map view. – Wolverine Dec 22 '15 at 09:35
2 Answers
You can use the google maps directions api (https://developers.google.com/maps/documentation/directions/#JSON) to find the shortest path. Then you have two options:
- Draw it in a GMSMapView
Decode the encoded polilyne returned by google with the Google maps SDK and translate it to a MKPolyline to show it in a MKMapView. Here's a snippet in Obj-c for the second option:
GMSPath *path = [GMSPath pathFromEncodedPath:encodedPolyline]; if (path.count != 0){ CLLocationCoordinate2D points[path.count]; for (NSInteger i = 0; i < path.count; i++){ CLLocationCoordinate2D coordinate = [path coordinateAtIndex:(NSUInteger) i]; points[i] = coordinate; } MKPolyline *p = [MKPolyline polylineWithCoordinates:points count:path.count]; [self.mapView addOverlay:p]; }
You have some limitation using the directions API, you can see it here: https://developers.google.com/maps/documentation/directions/usage-limits

- 7,796
- 1
- 20
- 47
-
but in this case , the route is identified by passing two points right(start and end)?.In my case I have multiple points and not sure which is the last point ,dynamically I need to find the last point which will be the longest distance from current location – user2577391 Dec 22 '15 at 10:42
-
Yes, you have to know the start and end of the route. You could calculate the distance between the current location and all the points using distanceFromLocation: and then use the longest as your end. – LorenzOliveto Dec 22 '15 at 10:55
You can use Directions API. You can plot multiple locations by setting a start point, waypoints(optional) and an endpoint. For example you are to plot 5 different locations, your current location(startpoint), then three waypoints or stopovers and the endpoint. Directions API automatically calculates distance based on the order in which you put the waypoints. Routes can recalculated if you use optimize:true
, by doing so it will rearrange the waypoints which will give you the shortest possible distance from your start point to the endpoint. This is only effective for a maximum of 8 places because of the number of limits of waypoints. There is a workaround for this, the idea is to make the last waypoint(end route) of the first route to be the start point of the next route. Here is a link for your reference and the code which is also in the reference.

- 1
- 1

- 1,444
- 8
- 10