Calculate your driving distance on iPhone

I need to find the distance between two points. I do not need to display directions on the map, I just need to calculate the distance that I need to use in my application.

Does MapKit allow this? Is there an alternative that can be used?

I can promote geocoding using CloudMade, but there seems to be no way to get the distance.

Appreciate any help.

+3
source share
3 answers

CloudMade also offers driving directions. If you are only interested in distance, just ignore the instructions.

The API call looks like this:

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

+3

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

Source: https://habr.com/ru/post/1729310/


All Articles