2013-10-03 35 views
0

我計算了2點(經度和緯度)之間的距離。我用distanceFromLocation:但這給了我空中距離。我畫了MKPolyline on MKMapview顯示道路路線。現在的問題是,我怎樣才能得到實際的道路距離,而不使用GOOGLE API如何計算mkmapview上的實際道路距離?

回答

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]]; 
    } 
+0

這使用Google API!問題是不使用Google Api。 – AlexWien

+0

由於我沒有其他選擇。因此這對我來說是最後的解決方案。 –

+0

嚴格地說,您不允許在非Google地圖上顯示來自Google的數據,這是他們的許可協議,但我不知道這是如何實施的。 – Craig

0

can't find the distance based on roads with the Apple SDK。如果你真正想要的,那麼你必須直接,如果你真行,只有使用的是iOS 7經過谷歌的API:

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

+0

kk ..但我不知道如何使用它。 –

+0

您需要導入Google MAp SDk。請仔細閱讀此文檔:** https://developers.google.com/maps/documentation/ios/?hl = fr-FR **以及其他樣本 –

2

,您可以通過MKDirections API的MKRoute。該路線具有distance財產,這是旅行距離(即不像烏鴉蒼蠅)。

+0

No.我正在使用iOS6。所以我按照這個搜索 –