Chapter 20. 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 types that 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 if you’re already importing the Map Kit framework.

Map Views

A map is displayed through a UIView subclass, an MKMapView. You can instantiate an MKMapView in code like any other view. Alternatively, in the nib editor, the Objects library contains an MKMapView object that you can drag into your interface as you design it; you might need to link to the MapKit framework manually (in the app target’s Link Binary With Libraries build phase) to prevent the app from crashing as the nib loads.

A map has a type, which is usually one of the following (MKMapType):

  • .standard

  • .satellite

  • .hybrid

(A further MKMapType, .mutedStandard, dims the map elements so that your additions to the map view stand out.)

Displaying a Region

The area displayed on the map is its region, an MKCoordinateRegion. This is a struct comprising two things:

center

A CLLocationCoordinate2D. The latitude and longitude of the point at the center of the region.

span

An MKCoordinateSpan. The quantity of latitude and longitude embraced by the region (and hence the scale of the map).

In this example, I’ll initialize the ...

Get Programming iOS 13 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.