Skip to Content
Programming iOS 10
book

Programming iOS 10

by Matt Neuburg
November 2016
Beginner to intermediate content levelBeginner to intermediate
800 pages
30h 12m
English
O'Reilly Media, Inc.
Content preview from Programming iOS 10

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. A map has a type, which is one of the following (MKMapType):

  • .standard

  • .satellite

  • .hybrid

The area displayed on the map is its region, an MKCoordinateRegion. This is a struct comprising a location (its center, a CLLocationCoordinate2D), describing the latitude and longitude of the point at the center of the region, 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 20-1):

let loc = CLLocationCoordinate2DMake(34.927752,-120.217608)
let span = MKCoordinateSpanMake(0.015, 0.015)
let reg = MKCoordinateRegionMake(loc, span)
self.map.region = reg
Figure 20-1. A map view showing a happy place

An MKCoordinateSpan ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming iOS 12

Programming iOS 12

Matt Neuburg
Programming iOS 11

Programming iOS 11

Matt Neuburg
Programming iOS 13

Programming iOS 13

Matt Neuburg

Publisher Resources

ISBN: 9781491970157Errata Page