Calculating driving distance in iPhone

末鹿安然 提交于 2019-12-10 10:15:46

问题


I need to find the driving distance between 2 locations. I do not need to display the directions in a map, just need to calculate the distance, which I need to use in my application.

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

I am able to get forward geo-coding using CloudMade, but there doesn't seem to be an option to obtain driving distance.

Appreciate any help.


回答1:


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

An 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

and the JSON includes

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

Source




回答2:


Found this in Google Groups, is that helpful?

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




回答3:


In my applications I used MKDirections to get driving (walking) distance between two location.

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);
              }
          }

 }];

if you have you location as an address, so you can use the method of CLGeocoder, which will give you latitude and longitude of your address

- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

If you compare a driving distance result using MKDirections with the one using Google Maps, you will see that they differ. I was searching regarding this matter and came across the following link http://www.macworld.co.uk/review/reference-education/apple-maps-vs-google-maps-3464377/

Even though Apple have been improving their map service, they still concede in accuracy to Google (IMO), at least in a question regarding the driving distance. So if accuracy is not highly important in your case, so you can follow along with Apple. Otherwise I would recommend checking Google API.



来源:https://stackoverflow.com/questions/2105875/calculating-driving-distance-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!