Domanda

I am developing an application in which I need to draw a route between two points using lat/long.

I have used apple API to get polylines and drawing it after decoding it.

Problem:

  1. Route is not in the middle of the road (Attached image_1) or route is misaligned

enter image description here

enter image description here

Below is the code:

    NSString* saddr = [NSString stringWithFormat:@"%@,%@",self.lat1.text,self.long1.text];

    NSString* daddr = [NSString stringWithFormat:@"%@,%@",self.lat2.text,self.long2.text];

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.apple.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];

    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

    NSError *error;
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];


    NSData *responseData = [apiResponse dataUsingEncoding:NSUTF8StringEncoding];


    NSError* error1;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                         options:NSJSONReadingMutableLeaves
                                                           error:&error1];
    NSLog(@"Error: %@\n%@",[error1 localizedDescription],[error1 localizedFailureReason]);


   if([[json objectForKey:@"status"] isEqualToString:@"OK"])
    {
        NSArray *routes1 = [json objectForKey:@"routes"];
        NSDictionary *route = [routes1 lastObject];

        if (route)
        {
            NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];

            routes = [self decodePolyLine:overviewPolyline];

            //NSLog(@"%@",[routes objectAtIndex:0]);

            [self updateRouteView];
            [self centerMap];
        }
    }


-(void) updateRouteView
{
 NSInteger numberOfSteps = routes.count;

CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
    CLLocation *location = [routes objectAtIndex:index];
    CLLocationCoordinate2D coordinate = location.coordinate;

    coordinates[index] = coordinate;
}

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];

}
È stato utile?

Soluzione 2

It's the same problem you described in your other question and it has the same solution. You are using data from two different sources, and the data is different.

Altri suggerimenti

decodePolyLine is totally depend n the latitude and longitude you get. So if you get proper latitude and longitude then this case will not happen. Another reason for that tis from iOS6 and above apple is using it's own map so may be they have different latitude and longitude from the Google map.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top