我已经计算了 2 个点(纬度和经度)之间的距离。我使用了distanceFromLocation:但这给了我空中距离。我在显示道路路线的MKMapview 上绘制了MKPolyline 。现在的问题是,我怎样才能在不使用GOOGLE API的情况下获得实际的道路距离?
问问题
1377 次
3 回答
2
如果您可以只使用 iOS 7,MKRoute
请从MKDirections
API 获取。路线有一个distance
属性,即行进距离(即不是像乌鸦飞的那样)。
于 2013-10-03T10:30:24.267 回答
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]];
}
于 2013-10-03T12:43:30.680 回答
0
你can't find the distance based on roads with the Apple SDK
。如果你真的想要,那么你必须直接通过谷歌API。:
于 2013-10-03T10:14:04.310 回答