【发布时间】:2013-10-09 23:43:51
【问题描述】:
我计算了 2 个点(纬度和经度)之间的距离。我使用了 distanceFromLocation: 但这给了我空中距离。我画了 MKPolyline 在 MKMapview 上显示道路路线。现在的问题是,我怎样才能在不使用 GOOGLE API 的情况下获得实际的道路距离?
【问题讨论】:
标签: ios google-maps mkmapview
我计算了 2 个点(纬度和经度)之间的距离。我使用了 distanceFromLocation: 但这给了我空中距离。我画了 MKPolyline 在 MKMapview 上显示道路路线。现在的问题是,我怎样才能在不使用 GOOGLE API 的情况下获得实际的道路距离?
【问题讨论】:
标签: ios google-maps mkmapview
如果您可以只使用 iOS 7,请从 MKDirections API 获取 MKRoute。该路线有一个distance 属性,它是行驶距离(即不是乌鸦飞的那样)。
【讨论】:
我的答案给出了实际距离。
- (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]];
}
【讨论】:
你can't find the distance based on roads with the Apple SDK。如果你真的想要,那么你必须直接通过谷歌API。:
【讨论】: