Introduction to MapKit in iOS 6 Tutorial

A tutorial that shows you how you can use MapKit in your iOS apps to show maps, drop pins, look up addresses, and more! By Matt Galloway.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Opening the Maps app

Wanna try out a cool new feature in iOS 6? You can now easily launch the Maps app right from within your app, with parameters to configure exactly what to show!

Remember that mapItem method you added to MyLocation? Well now you’re about to use it! Open ViewController.m and add the following code:

// Add to mapView:viewForAnnotation: after setting the image on the annotation view
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

// Add the following method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    MyLocation *location = (MyLocation*)view.annotation;
    
    NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
    [location.mapItem openInMapsWithLaunchOptions:launchOptions];
}

You’ve now made it so that in the callout when a pin is tapped on, there will be a button on the right hand side. When this is tapped, the mapView:annotationView:calloutAccessoryControlTapped: method is called. In this method, you grab the MyLocation object that this tap refers to and then launch the Maps app by calling the openInMapsWithLaunchOptions: method.

It’s as simple as that! You’ll notice that a dictionary is passed in to this method. This allows you to specify a few different options; here the directions key is set to driving. This will make the Maps app try to give driving directions from the current location to this pin. Neat!

I suggest you take a look at the various different options you can pass in the launch options dictionary. Also take a look at the class method on MKMapItem to allow you to pass multiple MKMapItem objects at the same time.

Now build & run the app, perform a refresh and tap on an arrest item. Then tap the blue button on the right and watch it launch the Maps app to show that arrest, with driving directions to it. Very cool :]!

Note: You may have gotten an error when opening the Maps app. If so, it’s likely because it couldn’t get driving directions for some reason. To fix that, simulate your location as being somewhere in the US (e.g. San Francisco) and try again. This time it’ll work and show you directions from downtown San Francisco right to the arrest!

Note: You may have gotten an error when opening the Maps app. If so, it’s likely because it couldn’t get driving directions for some reason. To fix that, simulate your location as being somewhere in the US (e.g. San Francisco) and try again. This time it’ll work and show you directions from downtown San Francisco right to the arrest!

Adding a Progress indicator

The App works fine so far, but it’d be better if we let the user know what’s going on when we pass the HTTP Request, a nice and easy MBProgressHUD will do that so let’s quickly add it in the ViewController.m:

// Add at the top of the file
#import "MBProgressHUD.h"

// Add right after [request startAsynchronous] in refreshTapped action method
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading arrests...";

// Add at start of setCompletionBlock and setFailedBlock blocks 
[MBProgressHUD hideHUDForView:self.view animated:YES];

Ahh… much more responsive! :]

Where To Go From Here?

Here is a sample project with all of the code we’ve developed in the above tutorial.

Now you know the basics of using Map Kit, but there’s a lot more you can do from here, including geocoding, adding custom map overlays, and more. A great place to go to for additional information is Apple’s Location Awareness Programming Guide.

To take this app further you may want to look into the MKMapItem class method I hinted at for opening the Maps app with multiple items. Perhaps add a button to the toolbar that opens the Maps app with all of the arrests currently shown. Also, why not take a look at the other launch dictionary options to control what happens when Maps opens.

If you want to learn more the new MapKit features in iOS 6, including registering your app as a routing provider, you should check out our new book iOS 6 by Tutorials. The book contains a chapter on MapKit that covers how you can launch Maps with various options, and how you can register your own app as a routing provider to give directions to users!

If you have any questions as you use MapKit in your apps, hints for other MapKit users, or are interested in using government data in your apps, please let me know in the forum discussion below! :]

This is a blog post by iOS Tutorial Team members Matt Galloway, Adam Burkepile, and Ray Wenderlich.