Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map SDK - Path not accurate

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 :

enter image description here

As you can see, the blue line (path) does not go at the destination.
How to fix this ?

like image 832
Nitish Avatar asked Jan 08 '16 05:01

Nitish


2 Answers

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.

enter image description here

like image 121
shpasta Avatar answered Oct 20 '22 05:10

shpasta


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

like image 24
Stefano Saitta Avatar answered Oct 20 '22 03:10

Stefano Saitta