计算iPhone的行驶距离
-
21-09-2019 - |
题
我需要找到两个位置之间的驾驶距离。我不需要在地图中显示说明,只需要计算我需要在应用程序中使用的距离即可。
MapKit允许这个吗?是否可以使用替代方法?
我能够使用CloudMade进行前进地理编码,但是似乎没有一个获得驾驶距离的选择。
感谢任何帮助。
解决方案
Cloudmade还提供行驶方向。如果您仅对距离感兴趣,只需忽略说明即可。
一个api-call看起来像这样:
和JSON包括
....
route_summary: {
total_distance: Distance in meters,...
其他提示
在Google组中找到了这一点,这有帮助吗?
http://groups.google.com/group/google-maps-api/msg/4dc2fad4f74e3314?pli=1
在我的应用中,我使用MKDirections在两个位置之间驾驶(步行)距离。
CLLocationCoordinate2D startCoordinates = YOUR_START_COORDINATES;
CLLocationCoordinate2D endCoordinates = YOUR_END_COORDINATES;
MKPlacemark *startPoint = [[MKPlacemark alloc] initWithCoordinate:startCoordinates];
MKPlacemark *endPoint = [[MKPlacemark alloc] initWithCoordinate:endCoordinates];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startPoint];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPoint];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = startItem;
request.destination = endItem;
request.transportType = MKDirectionsTransportTypeAutomobile; //here you can choose a transport type you need
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (response) {
for (MKRoute *route in response.routes) {
NSLog(@"Distance : %f", route.distance);
}
}
}];
如果您将您的位置作为地址,因此您可以使用clgeocoder的方法,这将为您提供地址的纬度和经度
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
如果使用MKDirections与使用Google Maps的MKDirections进行比较,则会发现它们不同。我正在搜索此事,并遇到以下链接 http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/
即使苹果一直在改善其地图服务,但至少在有关驾驶距离的问题上,他们仍然对Google(IMO)的准确性承认。因此,如果在您的情况下准确性并不重要,因此您可以跟随Apple。否则,我建议检查Google API。
不隶属于 StackOverflow