Reputation: 6839
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.
Upvotes: 0
Views: 4682
Reputation: 14237
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;
}
Upvotes: 2
Reputation: 2535
you have to just pass origin and destination co-ordinate and then parse the result.
Upvotes: 1
Reputation: 12405
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
Upvotes: 2
Reputation: 7733
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.
Upvotes: 4
Reputation: 3485
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
Upvotes: 4