Map Kit and Current Location

A device may have sensors that can determine its current location (Chapter 35). Map Kit provides simple integration with these facilities. Keep in mind that the user can turn off these sensors or can refuse your app access to them (in the Settings app, under Privacy → Location Services), so trying to use these features may fail. Also, determining the device’s location can take time.

You can ask an MKMapView in your app to display the device’s location just by setting its showsUserLocation property to YES. If your app has not been granted or denied access to Location Services, the system alert requesting authorization will appear. If access is granted, the map automatically puts an annotation at that location.

The userLocation property of the map view is an MKUserLocation, adopting the MKAnnotation protocol. It has a location property, a CLLocation, whose coordinate is a CLLocationCoordinate2D; if the map view’s showsUserLocation is YES and the map view has actually worked out the user’s location, the coordinate describes that location. It also has title and subtitle properties, plus you can check whether it is currently updating. You are free to supply your own annotation view to be displayed for this annotation, just as for any annotation.

Displaying the appropriate region of the map — that is, actually showing the part of the world where the user is located — is the responsibility of the map delegate’s mapView:didUpdateUserLocation: method:

- (void)mapView:(MKMapView ...

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.