Chapter 21. Maps

Your app can imitate the Maps app, displaying a map interface and placing annotations and overlays on the map. The relevant classes are provided by the Map Kit framework. You’ll need to @import MapKit. The classes used to describe locations in terms of latitude and longitude, whose names start with “CL”, come from the Core Location framework, but you won’t need to import it explicitly.

Displaying a Map

A map is displayed through a UIView subclass, an MKMapView. An MKMapView instance can be created in code or through the nib editor.

Warning

To instantiate an MKMapView from the nib, you’ll need to link your target explicitly to the MapKit framework (under Linked Frameworks and Libraries in the General tab when you edit the target); otherwise, your app will crash when the nib tries to load.

A map has a type, which is one of the following:

  • MKMapTypeStandard
  • MKMapTypeSatellite
  • MKMapTypeHybrid

The area displayed on the map is its region, an MKCoordinateRegion. This is a struct comprising a location (a CLLocationCoordinate2D), describing the latitude and longitude of the point at the center of the region (the map’s centerCoordinate), along with a span (an MKCoordinateSpan), describing the quantity of latitude and longitude embraced by the region and hence the scale of the map. Convenience functions help you construct an MKCoordinateRegion.

In this example, I’ll initialize the display of an MKMapView (self.map) to show a place where I like to go dirt biking (Figure 21-1):

Get Programming iOS 7, 4th Edition 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.