Mahesh Mahajan
Mahesh Mahajan

Reputation: 101

How to calculate actual road distance on mkmapview?

I have calculated distance between 2 points(latitude and longitude). I used distanceFromLocation: but this gives me aerial distance. I have drawn MKPolyline on MKMapview showing road route. Now the problem is that, how can i get the actual road distance without using GOOGLE API?

Upvotes: 0

Views: 1399

Answers (3)

Mahesh Mahajan
Mahesh Mahajan

Reputation: 101

I got my answer which gives me actual distance.

- (NSArray*)getRoutePointFrom:(myannotation *)origin to:(myannotation *)destination 
{
    NSString* sourcePoint = [NSString stringWithFormat:@"%f,%f",origin.coordinate.latitude, origin.coordinate.longitude];
    NSString* destinationPoint = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", sourcePoint, destinationPoint];
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

    NSError *error;//saddr
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"points:\"([^\"]*)\"" options:0 error:NULL];
    NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];
    NSString *encodedPoints = [apiResponse substringWithRange:[match rangeAtIndex:1]];

    NSRegularExpression *distRegEx=[NSRegularExpression regularExpressionWithPattern:@"tooltipHtml:\"([^\"]*)\""options:0 error:NULL];
    NSTextCheckingResult *distmatch=[tempregx firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];

    NSString *dist= [apiResponse substringWithRange:[temppmatch rangeAtIndex:0]];
    NSLog(@"dist in Km: %@",dist);
    return [self decodePolyLine:[encodedPoints mutableCopy]];
  }

Upvotes: 1

nevan king
nevan king

Reputation: 113777

If you're ok with only using iOS 7, get an MKRoute from the MKDirections API. The route has a distance property which is the travel distance (i.e. not as the crow flies).

Upvotes: 2

Kumar KL
Kumar KL

Reputation: 15335

You can't find the distance based on roads with the Apple SDK. If you really want ,then you must go through directly to google APIs.:

http://code.google.com/intl/fr-FR/apis/maps/index.html

Upvotes: 0

Related Questions