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 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 if you’re already importing the Map Kit framework.
Displaying a Map
A map is displayed through a UIView subclass, an MKMapView. You can instantiate an MKMapView from a nib or create one in code. If you’re going to create a map view using the nib editor (look for Map Kit View in the Library), it’s a good idea to switch on Maps in the Capabilities tab for your app target; this makes sure that your app links to the Map Kit framework and prevents a possible crash when the nib loads and the map view is instantiated.
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.)
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 ...
Get Programming iOS 12 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.