使用mapkit计算两个地址之间的驾驶距离?

问题描述 投票:6回答:4

是否可以使用iphone sdk中的mapkit来计算两个地址之间的行车距离?

iphone objective-c ios mapkit
4个回答
3
投票

如何使用Core Location框架:

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

你需要两个地址的纬度/经度。

编辑:在iOS 3.2中不推荐使用。请改用distanceFromLocation:方法。


2
投票

使用iOS7你get full directions within the API

看看这个教程Tutorial


1
投票

不,但如果您有经度/纬度,那么计算距离相当容易。当然这是这些点之间的数学距离。不是基于实际路线的实际驾驶或步行距离。


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/

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

© www.soinside.com 2019 - 2024. All rights reserved.