Chapter 8
How to Get Direction and Draw Route on Maps

Since the release of the iOS 7 SDK, the MapKit framework includes the MKDirections API which allows iOS developers to access the route-based directions data from Apple's server. Typically you create an MKDirections instance with the start and end points of a route. The instance then automatically contacts Apple's server and retrieves the route-based data.

You can use the MKDirections API to get both driving and walking directions depending on your preset transport type. If you like, MKDirections can also provide you alternate routes. On top of all that, the API lets you calculate the travel time of a route.

Again we'll build a demo app to see how to utilize the MKDirections API. After going through the chapter, you will learn the following stuff:

  • How to get the current user's location
  • How to use #available to handle multiple versions of APIs
  • How to compute the route and draw it on the map
  • How to use the segmented control
  • How to retrieve the route steps and display the detailed driving/walking instructions

The Sample Route App

I have covered the basics of the MapKit framework in the Beginning iOS 11 Programming with Swift book, so I expect you have some idea about how MapKit works, and understand how to pin a location on a map. To demonstrate the usage of the MKDirections API, we'll build a simple map app. You can start with this project template.

If you build the template, you should have an app that shows a list of restaurants. By tapping a restaurant, the app brings you to the map view with the location of the restaurant annotated on the map. If you have read our beginner book, that's pretty much the same as what you have implemented in the FoodPin app. We'll enhance the demo app to get the user's current location and display the directions to the selected restaurant.

Figure 8.1. The Food Map app running on iOS 10 and 11
Figure 8.1. The Food Map app running on iOS 10 and 11

To continue reading, please get the book here.

results matching ""

    No results matching ""