Displaying Maps

The Map Kit framework provides a set of classes for embedding maps into your apps.

Map Kit does not include any location sensing functionality, and so it is often used in conjunction with the Core Location framework.

Map Kit is not included by default in a new Xcode project. You need to make sure to properly link to the MapKit.framework in the Build Phases tab of your project settings if you want to use any classes starting with the MK prefix.

Displaying Maps

MKMapView is used to add a map view to your app. MKMapView extends UIView, so it inherits all of its properties. The easiest way to display a map view is to drag it to your main view in the Storyboard.

The map view has standard display type by default, but it can also have a satellite or hybrid type. This can be set in the Storyboard or using the mapType property.

By default, the map view zooms, scrolls, and does not display the user location. All these options can be edited in the Storyboard or by editing the zoomEnabled, scrollEnabled, and showsUserLocation properties respectively.

The region displayed by the map can be set and animated using setRegion:animated: method. The region is of MKCoordinateRegion type, which is a struct holding a CLLocationCoordinate2D for the center, and an MKCoordinateSpan for the span, which is itself a struct holding latitudeDelta and longitudeDelta, both of type CLLocationDegrees. An MKCoordinateRegion can easily be built using the MKCoordinateRegionMakeWithDistance(...) function.

Header Source


Map views can display hotspots in their view, which are called annotations. Any type of object can be used as an annotation, as long as it conforms to the MKAnnotation protocol. An MKAnnotation needs a coordinate, and can optionally have a title and a subtitle.

Header Source

Annotations can then be added to the map view using addAnnotation: or addAnnotations:.

Annotations can be removed from the map view using removeAnnotation: or removeAnnotations:.

Annotation Views

Annotations are drawn on the map view using an MKAnnotationView subclass. The default is the MKPinAnnotationView.

MKAnnotationView instances are created and assigned to an MKAnnotation in a way that is very similar to how UITableViewCell instances are created in a UITableView, using a reuse pool.

You can conform to the MKMapViewDelegate protocol and implement mapView:viewForAnnotation: to customize the annotation view.

mapView:didSelectAnnotationView: is called whenever an annotation view on the map is tapped.

If you also want the callout (info box with the title and subtitle) to appear on tap, you need to explicitly set canShowCallout to YES on the corresponding MKAnnotationView.

You can customize the callout by adding accessory views on the left and right using the leftCalloutAccessoryView and rightCalloutAccessoryView properties respectively. These can be set to any subclass of UIView. If they are set to a UIControl, mapView:annotationView:calloutAccessoryControlTapped: will be called on the delegate when they are tapped.

There are many more options for customizing MKAnnotationView. You can even completely replace the pin image by setting its image property.

  1. Paul Hegarty. Lecture 11 Slides. iPad and iPhone Application Development. Stanford, Nov 28 2011.

Comments are closed.