Display Placemarks with MapKit

With the MapKit API of the iOS SDK you easily can display locations in a map view. In this tutorial we will display Apple's HeadQuarters using forward geolocation and the placemark object. Placemarks are typical locations on the map, mostly generated by a CLGeocoder object. Placemark data includes information such as the country, state, city, and street address associated with the specified coordinate.

Open Xcode and create a new Single View Application. For product name, use PlaceMarkDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards is deselected and Use Automatic Reference Counting checkboxes are selected (Unit Tests will not be necessary for this project).

For this project we need two additional frameworks, go to project options and in the "Linked FrameWorks and Libraries" section add the CoreLocation and MapKit framework.

Go to ViewController.xib and drag a Map View onto the view. The view looks like this:

In ViewController.m add the following header files

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

In ViewController.m in the @interface section, create the following property

@property (nonatomic, weak) IBOutlet MKMapView *mapView;

Go back to Viewcontroller.xib and make the following connection:

  • Map View  -> mapView

In ViewDidLoad in ViewController.m add following lines:

self.mapView.mapType = MKMapTypeStandard;

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.323, -122.031);
MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);
MKCoordinateRegion regionToDisplay = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:regionToDisplay];

Here we set the Map style to Standard. the Center-coordinates are the latitude and longitude of Cupertino. We only reveal a small portion of the map with span. (1.0 is 111km wide), so only about 20km is visible. Finally we add this region to the map view.

Build and Run, you should see the following map

Add the following lines at the bottom of the previous code in ViewDidLoad

NSString *address = @"1 Infinite Loop, CA, USA"; 
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){
// Check for returned placemarks
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];

MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
[self.mapView addAnnotation:placemark];

First we put the address of Apple's HQ into a String and we create a CLGeocoder object. Then with the geocodeAddressString:completionHandler method we put the returned data into a PlaceMark object. This placemark is added as an annotation to the map view.

Build and Run, Press the red pin on the map and you will see the annotation of this placemark.

You can download the source code of the PlaceMarkDemo at the ioscreator repository on github.