Reputation: 1081
Please do not mark as duplicate without reading the whole question
Is there any way to get the length of a driven route between two CLLocation
points in iOS, without showing the map?
Not as the crow flies!!! But like in the MK driving mode
I cannot use Google since, since I will have to display the results on a Google-Map.
I found this piece of code, but this only works when showing an iOS-Map:
-(void)showMapRouteFromCurrentLocation
{
CLLocationCoordinate2D currentUserCoordinate = YourCoordinate;
//currentUserCoordinate.latitude= +53.509980;
//currentUserCoordinate.longitude = -0.133700;
MKPlacemark *place = [[MKPlacemark alloc]
initWithCoordinate:currentUserCoordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
NSDictionary *routeOption = [[NSDictionary alloc] initWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];
[mapItem openInMapsWithLaunchOptions:routeOption];
}
But that is not what I need.
Upvotes: 2
Views: 2740
Reputation: 14237
Although Ken gave a good answer, I will also add the way you transform a coordinate into a MKMapItem:
+ (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: 0
Reputation: 4077
As brainray said, from iOS7, you can do the following:
MKDirectionsRequest *routeRequest = [[MKDirectionsRequest alloc] init];
routeRequest.transportType = MKDirectionsTransportTypeAutomobile;
[routeRequest setSource:startPoint]; // startPoint is an MKMapItem with the starting lat/long coordinate
[routeRequest setDestination :endPoint]; // endPoint an MKMapItem with the ending lat/long coordinate
MKDirections *routeDirections = [[MKDirections alloc] initWithRequest:routeRequest];
[routeDirections calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * routeResponse, NSError *routeError)
{
// Check for error and then do something with the route...
MKRoute *route = routeResponse.routes[0];
}];
Upvotes: 6
Reputation: 12884
With iOS7 you get full directions within the API
Take a look at this tutorial Tutorial.
Upvotes: 2
Reputation: 149
give latitude2,lang of destination and latitude,longitude of first place.
this function return distance in kilometer.
-(double)distanceFilter:(double) latitude2 :(double)lang{
double distance=(((acos(sin((latitude*M_PI/180)) * sin((latitude2*M_PI/180))+cos((latitude*M_PI/180)) * cos((latitude2*M_PI/180)) * cos(((longitude- lang)*M_PI/180))))*180/M_PI)*60*1.1515*1.609344);
return distance;
}
Upvotes: -3
Reputation: 954
As far as I know this is not possible using Apple's APIs. You would need to use a 3rd party system to ask for directions between two points. Like google, but they often have other restrictions, like displaying it on their maps.(as you mentioned).
Not sure if they have similar requirements but you could check out Map Quest's Api
Upvotes: 3