9.10. Displaying Directions on the Map
Problem
You want to display directions on a map to show the user how to get from point A to point B.
Solution
Instantiate an object of type MKDirections
and issue the calculateDirectionsWithCompletionHandler:
instance method of that object. The completion handler will get called
and will pass you an object of type MKDirectionsResponse
. Use the directions
response object to open the Maps app on your device, as you will soon
learn.
Discussion
Directions to walk or drive can be displayed only in the Maps app
on a device, so you cannot display them inside an instance of a map view
in your app. The way we will go about displaying directions is very
straightforward. In order to display the directions on the Maps app, we
need to create an instance of the MKDirections
class. This class will require us to have already prepared an
instance of the MKDirectionsRequest
.
In addition, to create the directions request, you need to
create instances of the MKMapItem
.
Every map item will represent a point on the map. So the bottom line is
that if you want to display directions from point A to point B on the
map, you need to represent them as map items, create a request out of
that, and then use the MKDirections
class to receive the directions. After receiving the directions, you
have two choices:
Process the directions yourself. For instance, using the technique that you learned earlier in this chapter (see Recipe 9.4), you may want to retrieve all the gas stations ...
Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.