Use mapkit to calculate the distance between two addresses?

Is it possible to use mapkit in iphone sdk to calculate the distance between two addresses?

+6
source share
4 answers

How about using the Core Location platform:

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

You will need latitude / longitude for both addresses.

Change: not recommended in iOS 3.2. Use the method instead distanceFromLocation:.

+3
source

With iOS7, you get complete directions in the API

Take a look at this tutorial tutorial .

+2
source

, /, . , . , .

+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, , . http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/

Although Apple is improving its mapping service, they still agree to the accuracy of Google (IMO), at least in terms of travel distance. Therefore, if accuracy is not very important in your case, you can follow along with Apple. Otherwise, I would recommend checking out the Google API.

0
source

All Articles