The completionHandler
block called by geocodeAddressString
is asynchronous.
This means that the code that tries to get the directions from the current location to _destination
runs immediately after the geocodeAddressString
line and executes before _destination
is set.
So when mapItem
is created, it is using _destination
still set to nil
(the geocoder block hasn't finished and set _destination
yet).
Trying to get directions from the current location to nil
results in the get directions error.
The simplest fix is to move the code that gets the directions request inside the completionHandler
block of geocodeAddressString
(right after _destination
is actually set):
[self.mapView addAnnotation:placemark];
_destination = placemark;
//move the code that gets the directions here,
//inside this block...
MKMapItem *mapItem = [[MKMapItem alloc] init...
...
[directions calculateDirections...
}