【问题标题】:How to calculate actual road distance on mkmapview?如何在 mkmapview 上计算实际道路距离?
【发布时间】:2013-10-09 23:43:51
【问题描述】:

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

【问题讨论】:

    标签: ios google-maps mkmapview


    【解决方案1】:

    如果您可以只使用 iOS 7,请从 MKDirections API 获取 MKRoute。该路线有一个distance 属性,它是行驶距离(即不是乌鸦飞的那样)。

    【讨论】:

    • 没有。我正在使用iOS6。所以我正在按照那个搜索
    【解决方案2】:

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

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

    【讨论】:

    • 这使用谷歌API!问题是不要使用 Google Api。
    • 因为我没有其他选择。因此,这对我来说是最后的解决方案。
    • 严格来说,您不能在非 Google 地图上显示来自 Google 的数据,这是在他们的许可协议中,但我不知道这是如何强制执行的。
    • 嗨,mahi,什么是 temppmatch 对象?在这段代码中。
    【解决方案3】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2012-04-02
    相关资源
    最近更新 更多