如何计算mkmapview上的实际道路距离?

时间:2013-10-03 10:09:11

标签: ios google-maps mkmapview

我计算了2点(纬度和经度)之间的距离。我使用 distanceFromLocation:,但这给了我空中距离。我画了 MKPolyline MKMapview 上显示道路路线。现在的问题是,如何在不使用 GOOGLE API 的情况下获得实际道路距离?

3 个答案:

答案 0 :(得分:2)

如果您只使用iOS 7,请从MKDirections API获取MKRoute。该路线具有distance属性,即旅行距离(即不像乌鸦一样)。

答案 1 :(得分:1)

我的答案给了我实际的距离。

- (NSArray*)getRoutePointFrom:(myannotation *)origin to:(myannotation *)destination 
{
    NSString* sourcePoint = [NSString stringWithFormat:@"%f,%f",origin.coordinate.latitude, origin.coordinate.longitude];
    NSString* destinationPoint = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", sourcePoint, destinationPoint];
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

    NSError *error;//saddr
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"points:\"([^\"]*)\"" options:0 error:NULL];
    NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];
    NSString *encodedPoints = [apiResponse substringWithRange:[match rangeAtIndex:1]];

    NSRegularExpression *distRegEx=[NSRegularExpression regularExpressionWithPattern:@"tooltipHtml:\"([^\"]*)\""options:0 error:NULL];
    NSTextCheckingResult *distmatch=[tempregx firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];

    NSString *dist= [apiResponse substringWithRange:[temppmatch rangeAtIndex:0]];
    NSLog(@"dist in Km: %@",dist);
    return [self decodePolyLine:[encodedPoints mutableCopy]];
  }

答案 2 :(得分:0)

can't find the distance based on roads with the Apple SDK。如果你真的想要,那么你必须直接进入谷歌API。:

  

http://code.google.com/intl/fr-FR/apis/maps/index.html