I am trying to show path between two locations but it is not accurate.
if(center.latitude != 0 && center.longitude != 0)
{
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
center.latitude,
center.longitude,
center1.latitude,
center1.longitude,
KGoogleMapServerKey];
NSError *error;
NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString] options:NSDataReadingUncached error:&error] options:NSJSONReadingMutableContainers error:&error];
if(json[@"routes"] != nil && [json[@"routes"] count] > 0)
{
GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
singleLine.strokeWidth = 7;
singleLine.strokeColor = [UIColor colorWithRed:56.0/255.0 green:163.0/255.0 blue:249.0/255.0 alpha:1.0];
singleLine.map = mapViewGoogle;
}
This is the path I am getting :
As you can see, the blue line (path) does not go at the destination.
How to fix this ?
Your route is too long. In each response from Google Directions API number of points is limited. In your example you received N points, drew route with them and zoomed mapView to area, where only four points placed.
To display always accurate route you should after each map zooming re-calculate route (including midpoints to keep route the same).
For example, full route on your screen has 50 points. You are zooming the map, so only 4 points displayed (your screenshot). Now take #1 as start, #5 as finish and #2 #3 #4 as midpoints and send new request to find route for these points. You'll receive new 50 points, and with them you can draw this part of route more accurately.
The point of this is that there is no route for that destination, as you can see here : Gmap of your Example
I guess that your best bet is use the same approach that Gmaps already use, i mean using dotted/dashed polyline from the last point you get directions to the actual point.
You can find out here couple examples of out to implement thy type of polyline:
Creating a dashed using IOS SDK
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