Displaying Traffic, Scale and Compass Indicators on the Map - Maps and Location - iOS 9 Swift Programming Cookbook (2015)

iOS 9 Swift Programming Cookbook (2015)

Chapter 8. Maps and Location

8.5 Displaying Traffic, Scale and Compass Indicators on the Map

Problem

You want to display traffic as well as the little compass and scale indicators on the map view.

Solution

Set the following properties of your map view to true:

§ showsCompass

§ showsTraffic

§ showsScale

Discussion

Place a map view on your view and set the appropriate constraints on it so that it stretches across the width and height of your view controller’s view. This is really optional, but useful so the user can see the map view properly on all devices. Then follow what I talked about in Recipe 8.3 to place an annotation on the map. Write a code similar to the following in a method such as viewDidLoad:

map.showsCompass = true

map.showsTraffic = true

map.showsScale = true

The results will be similar to those shown in Figure 8-4. The scale is shown on top left and the compass on the top right. You have to rotate the map for the compass to appear.

Figure 8-4. Map with scale, compass, and traffic

See Also