- Mar 29, 2012
Working with Map Kit
Now let’s turn from working with data oriented location objects to maps. The Map Kit framework is rather extensive and provides the necessary views and controls for displaying map data. The primary view in the Map Kit framework is MKMapView, which is a subclass of UIView, and automatically renders Google Maps data based on the relative location of a visible map view rect.
Tracking Location with Map Kit
So you know that the MKMapView render’s map data and provides the same gesture-based interaction seen in the native Maps application. You also know that you can use the standard location service to track a user’s location. Fortunately, tracking a user’s position is a common enough use case that both Map Kit and Core Location offer this capability. The benefit of Map Kit’s tracking services is they will automatically track a user’s location and indicate that location on the map using the famous Google Maps blue tracking ball seen in the native Maps app. As accuracy changes, the region circle around this ball will automatically adjust just as it does in the native app.
To enable tracking on an MKMapView, simply set the Boolean property showsUsersLocation. When set to YES, the MKMapView will first prompt the user with the same Core Location permission dialog. If authorization is approved, the MKMapView will automatically animate the changes in a user’s location and the accuracy of the determination.
The MKMapView also manages a delegate property that it uses to communicate location update information through various methods. These methods are defined in the protocol MKMapViewDelegate and can be used to update necessary map information (such as reload overlays and annotations). The delegate method relevant to location updates is mapView:didUpdateUserLocation: which passes in an MKUserLocation object.
The MKUserLocation object is very handy. Unlike monitoring location with Core Location, the MKMapView can be configured to provide both heading and motion in a single delegate method based on the tracking mode defined by its userTrackingMode property. The possible values of this property are
When the tracking mode is set to follow with heading, the MKUserLocation object will contain both a CLLocation object and a CLHeading object. If the tracking mode is set to just follow, the MKUserLocation object passed to the delegate will only contain the location.