1

我正在开发一个应用程序,我需要在其中使用 lat/long 在两点之间绘制一条路线。

我已经使用苹果 API 来获取折线并在解码后绘制它。

问题:

  1. 路线不在路中间(附图片_1)或路线未对齐

在此处输入图像描述

在此处输入图像描述

下面是代码:

    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];

}
4

2 回答 2

1

decodePolyLine完全取决于您获得的纬度经度。因此,如果您获得适当的纬度经度,则不会发生这种情况。iOS6及更高版本苹果的另一个原因是使用它自己的地图,所以可能是它们与谷歌地图的纬度经度不同。

于 2013-04-15T06:25:39.590 回答
0

这与您在其他问题中描述的问题相同,并且具有相同的解决方案。您正在使用来自两个不同来源的数据,并且数据不同。

于 2013-04-14T23:52:18.430 回答