Search code examples
objective-cmapkitcllocationcoordinate2d

I'm getting a wrong expectedTravelTime from MKRoute


I'm trying to calculate the expectedTravelTime in my app, but I get different values when compared to GoogleMaps.

Here is my code:

NSArray *startLocationArray = [startLocation componentsSeparatedByString:@","];
NSArray *destinationArray = [destination componentsSeparatedByString:@","];

MKPlacemark *placemarkStartLocation = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([startLocationArray[0] intValue],
                                                                                            [startLocationArray[1] intValue]) addressDictionary:nil];
MKMapItem *mapItemStartLocation = [[MKMapItem alloc] initWithPlacemark:placemarkStartLocation];

MKPlacemark *placemarkDestination = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([destinationArray[0] intValue],
                                                                                            [destinationArray[1] intValue]) addressDictionary:nil];
MKMapItem *mapItemDestination = [[MKMapItem alloc] initWithPlacemark:placemarkDestination];

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:mapItemStartLocation];
[request setDestination:mapItemDestination];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
[request setRequestsAlternateRoutes:NO];
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
__block MKRoute *route;

[directions calculateDirectionsWithCompletionHandler:
 ^(MKDirectionsResponse *response, NSError *error)
{
    if ( ! error && [response routes] > 0)
    {
        route = [[response routes] objectAtIndex:0];

        double exceedingTime = fmod(route.expectedTravelTime, 60);
        eta = (route.expectedTravelTime - exceedingTime)/60;
        int iEta = (int)eta;

        lblEta.text = [NSString stringWithFormat:@"%im", iEta, nil];
    }
}];

startLocation and destination are passed outside the method, and their values are:

startLocation = 37.776402,-122.408470
destination = 37.831287,-122.485519

I get:

route.distance = 1513 (meters)
route.expectedTravelTime = 491 (should be seconds, so finally 8 minutes)

but on GoogleMaps I get more than 30 minutes of expectedTravelTime and more than 9 miles of distance

Where am I going wrong?


Solution

  • The problems are these conversions which use intValue instead of doubleValue:

    [startLocationArray[0] intValue], [startLocationArray[1] intValue]
    
    [destinationArray[0] intValue], [destinationArray[1] intValue]
    


    The start and destination coordinates have a fractional part (e.g. 37.776402) and so you must use doubleValue (which matches the defined types of latitude and longitude in the CLLocationCoordinate2D struct).


    Using intValue, the code is getting the route from 37, -122 to 37, -122 which is a short trip inside a golf course in Santa Cruz, CA.


    Change intValue in the above lines to doubleValue.