Search code examples
iosroutesrequestdistancedirections

Why can't I get a MKDirectionResponse


I am trying to get a Route between two points to get the walking distance between the two points. But I always end up getting the following error:

Error:

/SourceCache/ProtocolBuffer/ProtocolBuffer-225/Runtime/PBRequester.m:799 server (https://gsp-ssl.ls.apple.com/directions.arpc) returned error: 400
2015-08-12 16:19:34.354 projectName[2995:597568] RESPONSE: (null)

This is my code:

-(void)viewDidLoad{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];


    MKPlacemark *spot1 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.21299581976029, 4.404984000000013) addressDictionary:nil];
    MKPlacemark *spot2 = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(51.20173651985178, 4.450245532177766) addressDictionary:nil];


    MKMapItem * startItem = [[MKMapItem alloc] initWithPlacemark:spot1];
    MKMapItem * destinationItem = [[MKMapItem alloc] initWithPlacemark:spot2];

    NSLog(@"START: %@",startItem);
    NSLog(@"DESTINATION: %@",destinationItem);

    MKDirectionsRequest * directionsRequest = [[MKDirectionsRequest alloc] init];
    [directionsRequest setSource:startItem];
    [directionsRequest setSource:destinationItem];
    [directionsRequest setTransportType:MKDirectionsTransportTypeWalking];
    MKDirections *direction = [[MKDirections alloc] initWithRequest:directionsRequest];

    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        NSLog(@"RESPONSE: %@",response);

    }];
}

I tested with different internet connection to make sure this wasn't the problem. I also followed all the steps at Apple's Documentation to configuring my App to accept directions requests but nothing helped.


Solution

  • [directionsRequest setSource:destinationItem]; should be [directionsRequest setDestination:destinationItem];

    You are setting the source twice (or rather, overwriting the first value with the second). Then you are requesting directions without a destination and of course there is now way to compute that so you get an error.