Displaying a Map

A map is displayed through a UIView subclass, an MKMapView. The map is potentially a map of the entire world; the map view is usually configured to display a particular area. An MKMapView instance can be created in code or through the nib editor. 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 (map) to show a place where I like to go dirt biking (Figure 34-1):

CLLocationCoordinate2D loc =
    CLLocationCoordinate2DMake(34.924365,-120.217372);
MKCoordinateSpan span = MKCoordinateSpanMake(.015, .015);
MKCoordinateRegion reg = MKCoordinateRegionMake(loc, span);
self.map.region = reg;
A map view showing a happy place
Figure 34-1. A map view showing a happy place

An MKCoordinateSpan is described in degrees of latitude and longitude. It may be, however, that what you know is the region’s proposed dimensions in meters. To convert, call MKCoordinateRegionMakeWithDistance ...

Get Programming iOS 6, 3rd 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.