简体   繁体   中英

Calculate distance between two location based on roads

I have two MKCoordinateRegion objects. Based on values from those objects I make two annotations on map. Then I calculate distance between those two locations:

CLLocationCoordinate2D pointACoordinate = [ann coordinate];
    CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];  

    CLLocationCoordinate2D pointBCoordinate = [ann2 coordinate];
    CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];  

    float distanceMeters = [pointBLocation distanceFromLocation:pointALocation];

    distanceMeters = distanceMeters / 1000;

But I ma not sure that values I get is correct.
Are those values air distance?
Is it possible to get distance based on roads ?
I need distance that user must pass with car.

Theses values are air distance.

You can't find the distance based on roads with the Apple SDK. Try to ask directly to google APIs http://code.google.com/intl/fr-FR/apis/maps/index.html

Use CLLocation instead of CLLocationCoordinate:-

CLLocation has an init method named

-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude. 

Then use

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

to get the distance between two CLLocation objects on Road.

The Distance you will get is in kilometers.

Since iOS7 you can get that info with this:

+ (void)distanceByRoadFromPoint:(CLLocationCoordinate2D)fromPoint
                        toPoint:(CLLocationCoordinate2D)toPoint
              completionHandler:(MKDirectionsHandler)completionHandler {

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    request.transportType = MKDirectionsTransportTypeAutomobile;

    request.source = [self mapItemFromCoordinate:fromPoint];
    request.destination = [self mapItemFromCoordinate:toPoint];

    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * routeResponse, NSError *routeError) {

         MKRoute *route = [routeResponse.routes firstObject];
         CLLocationDistance distance = route.distance;
         NSTimeInterval expectedTime = route.expectedTravelTime;

         //call a completion handler that suits your situation

     }];    

   }


+ (MKMapItem *)mapItemFromCoordinate:(CLLocationCoordinate2D)coordinate {

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
    MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];

    return item;

}

what @coolanilkothari says is almost correct almost except for the fact that getDistanceFrom is deprecated in ios 3.2. This is what the apple docs have to say..

getDistanceFrom:

Returns the distance (in meters) from the receiver's location to the specified location. (Deprecated in iOS 3.2. Use the distanceFromLocation: method instead.) - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location Parameters

location

 The other location. 

Return Value

The distance (in meters) between the two locations. Discussion

This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations. Availability

 Available in iOS 2.0 and later. Deprecated in iOS 3.2. 

Declared In CLLocation.h

you have to just pass origin and destination co-ordinate and then parse the result.

http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Vancouver+BC&destinations=San+Francisco&sensor=false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM