MapKit and iOS

This is going to be a simple tutorial on using MapKit in iOS 5 with storyboard and ARC.

Open Xcode and start a new project (File->New->Project)  Under iOS select Application and then Single View Application.

Click Next and then set Product Name to FindMe, put anything for Company Identifier.  Make sure the Device Family is set to iPhone and that both Use Storyboards and Use ARC are selected.

Click Next and save the project.

First, we need to add the MapKit framework to our project.  Select the FindMe project, then select the FindMe target and then Build Phases tab.  Click on the Link Binary With Libraries to see what frameworks are added to the current project.

To add a new framework, click on the + button under the Link Binary With Libraries section.  A new sheet appears.  Find the MapKit.framework and add it to the project.

Next, we need to add a MapView to the storyboard.  Open MainStoryboard.storyboard, and drag a Map View from the object library to the storyboard.

We need to add a property outlet that connects to the MapView.  We also need to import the MapKit framework.  Open, ViewController.h and do the following changes.

Open, ViewController.m and synthesize the mapView property.

To finish setting up the outlet we need to connect it to the Map View.  Open MainStoryboard.storyboard, right click on the View Controller and drag from the mapView under outlets to the MKMapView.  This is what the pop up should look like when you are finished.

We also need to set the MapView delegate to the ViewController class.  Right click on Map View and from the pop up drag from the delegate under outlets to the View Controller.

If the delegate of MKMapView is set to the ViewController class, the class needs to implement the MKMapViewDelegate protocol.  Open ViewController.h and add <MKMapViewDelegate> at the end of the @interface line.

All that is left to do is to set the showUserLocation property for the mapView to YES, and implement the method of the protocol that tells us when the location was updated.  Open, ViewController.m and add [self.mapView setShowsUserLocation:YES]; to the viewDidLoad method and implement -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation  When the location is updated the map zooms to the new region.

Press the Run button or go to Product->Run.

You can simulate the location in the simulator and the map is updated and zoomed to the new location.

You can download the code used in this tutorial from github.