在iPhone中计算行驶距离

时间:2010-01-20 23:31:14

标签: iphone objective-c mapkit cloudmade

我需要找到2个地点之间的行车距离。我不需要在地图中显示方向,只需要计算距离,我需要在我的应用程序中使用。

MapKit是否允许这样做?有没有可以使用的替代方案?

我可以使用CloudMade进行地理编码,但似乎没有选择来获得行驶距离。

感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

CloudMade还提供行车路线。如果您只对距离感兴趣,请忽略说明。

API-Call如下所示:

  

http://navigation.cloudmade.com/YOUR-API-KEY-GOES-HERE/api/0.3/47.25976,9.58423,47.26117,9.59882/car/shortest.js

和JSON包括

....
route_summary: {
total_distance: Distance in meters,...

Source

答案 1 :(得分:2)

在Google网上论坛中发现这个有用吗?

http://groups.google.com/group/google-maps-api/msg/4dc2fad4f74e3314?pli=1

答案 2 :(得分:0)

在我的应用程序中,我使用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地图的驾驶距离结果进行比较,您会发现它们有所不同。我正在搜索此事并发现以下链接http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/

尽管Apple一直在改进他们的地图服务,但他们仍然准确地向Google(IMO)承认,至少在有关行驶距离的问题上。因此,如果准确性在您的情况下不是非常重要,那么您可以跟随Apple。否则,我建议您检查Google API。

相关问题