MapKit Example in iPhone

In this application we will see how to map display and how to point out any location using latitude and longitude.

Step 1: Create a View base application using template. Give the application name “MapKitDisplay”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the MapKitDisplayViewController class for you. Expand Resources and notice the template generated a separate nib, MapKitDisplay ViewController.xib, for the “ MapKitDisplay”.

Step 4: We need to add NSObject class in the project. Select Classes -> Add -> New File -> Cocoa Touch Class -> Objective C class -> select NSObject from the Subclass of. Give the file name “DisplayMap”.

Step 5: We have added two framework in the project. Select Frameworks -> Add -> Existing Frameworks -> Add CoreLocation.framework and MapKit.framework.

Step 6: In the MapKitDisplayViewController.h file, we have import MapKit framework, and define MKMapViewDelegate protocol in the file, also add Outlet with a pointer to the MkMapView class. So make the following changes in the file.


#import <uikit/UIKit.h>

@class DisplayMap;

@interface MapKitDisplayViewController : UIViewController {

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



Step 7: Double click the MapKitDisplayViewController.xib file and open it to the Interface Builder. First drag the MapView from the library and place it to the view window. Connect File’s Owner icon to the View icon and select view. Connect File’s Owner icon to the MKMapView and select mapView. Now save the MapKitDisplayViewController.xib file, close it and go back to the Xcode.

Step 8: Open the MapKitDisplayViewController.m file and make the following changes in the file:


- (void)viewDidLoad {
[super viewDidLoad];

[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; = 22.569722 ; = 88.369722;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];

[mapView setDelegate:self];

DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @" Kolkata";
ann.subtitle = @"Mahatma Gandhi Road";
ann.coordinate =;
[mapView addAnnotation:ann];

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id )annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
static NSString *defaultPinID = @"";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
else {
[mapView.userLocation setTitle:@"I am here"];
return pinView;



We define here first, coordinate regions to zeros, Then we enter coordinates of our place that, Kolkata (Mahatma Gandhi Road), define the latitude and longitude of this place. We create an instantiate of DisplayView object and add it to our map. To do this, we add the delegate function that will display the annotations on to our map. We start by having DisplayView name a pointer we’ll call “ann.”

Step 8: In the DisplayView.h file , we have import
<mapKit/MKAnnotation.h> and set CLLocation class reference to incorporate the geographical coordinates and altitude of our device. So make the following changes in the file.


#import <foundation/Foundation.h>

@interface DisplayMap : NSObject {

CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;



Step 9: Open the DisplayView.m file and make the following changes in the file:


#import "DisplayMap.h"

@implementation DisplayMap

@synthesize coordinate,title,subtitle;

[title release];
[super dealloc];



Step 10: Now compile and run the application on the simulator.

You can Download SourceCode from here MapKitDisplay

Leave a Comment:

prashanth says July 15, 2010

this is prashanth …and i have been started i phone development from last few months….i need some help for my app…
i want to draw on already exited image in iphone ..and save back to same location ………can any one help me……

Luke says July 21, 2010

Thank you. This code was very helpful.

GJ says August 4, 2010

thanks for the tutorial.

how would you display more than one annotation on a map. if you had a few different locations for example.

SD says January 4, 2011

hi Sushant
thanks for the tutorial.

I have done all this stuff regarding corelocation in this way. But unable to do the street view. Can anybody help me how do i implement street view in my app.

Please help me out….

Macheo says January 19, 2011

Deze code is voor fulscreen movie!

Stap 1:
Maak een mediaplayer.framework aan

Stap 2:
Zet deze code in je .h view:

UIWindow *window;


@property (nonatomic, retain) IBOutlet UIWindow *window;

Stap 3:
Zet deze code in je .m view:


-(IBAction)playMovie {
NSString *filepath = [[NSBundle mainBundle] pathForResource:@”Jou movie” ofType:@”Type movie”];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[moviePlayerController.moviePlayer play];
[window addSubview:moviePlayerController.view];


Ik hoop dat je hier iets aan hebt!

Groetjes Macheo

Farhan says January 23, 2011

Brilliant tutorial,

Its easy and simple helped me alot. in the same code if i want to add multiple pins and annotation any help please. I am stuck.

Phuoc Linh says January 25, 2011


zull says March 30, 2011

hye, thanks for the tutorial.
i have some problem. How i want to add more red pin in different places??
anyone plz help…

Warmup says April 10, 2011

I’ve just dowloaded XCode v4
Having some troubel to follow your example would it be possible to specify howto connect “STEP 7” in xcode 4.

I can get the map map up and running but it is not centering or showingen any annotations.

-tnx for a cool site

Warmup says April 10, 2011

I’ve just dowloaded XCode v4
Having some troubel to follow your example would it be possible to specify howto connect "STEP 7" in xcode 4.

I can get the map map up and running but it is not centering or showingen any annotations.

-tnx for a cool site

Gautam says April 13, 2011

Thanx , Tutorial is very useful !!!!

Oriolus says April 27, 2011

thanks you, this is very helpful!!!

Ambo says May 17, 2011

Help please, i did everything as it says and get error saying:
receiver ‘DisplayMap’ is a forward class and corresponding @interface may not exist

and wont build :S

    Sushant says May 18, 2011

    Please checked

    #import “MapKitDisplayViewController.h”
    #import “DisplayMap.h” //is it import MapKitDisplayViewController.m file?

    next checked in DisplayMap.h file

    #import // Are you import this in the file?
    Just checked and let us know if you have any problem.

Gary Benna says June 17, 2011

Doesn’t work got these messages

DisplayMap *ann = [[DisplayMap alloc] init]; Receiver ‘DisplayMap’ is a forward class and corresponding @interface may not exist.
ann.title = @”Kolkata”; Request for member ‘title’ in something not a structure or union
ann.subtitle = @”Mahatma Gandhi Road”; Request for member ‘subtitle’ in something not a structure or union
ann.coordinate =; Request for member ‘coordinate’ in something not a structure or union
[mapView addAnnotation:ann]; Type ‘DisplayMap *’ does not conform to the MKAnnotation protocol.

I am using sdk 4.0 for iphone

Any suggestions what to do to fix this.

    Sushant says June 18, 2011

    Please checked

    You need to import DisplayMap.h file in the MapKitDisplayViewController.m file. It should be look like below:

    #import “MapKitDisplayViewController.h”
    #import “DisplayMap.h” //import this file.

    If you have still problem then let us know.

Gary Benna says June 17, 2011

Sorry about the multiple entriies it said invalid security code. Then I imported DisplayMap.h and everything works fine. Thanks gary

John says June 17, 2011

Like most of the samples out there this is a simply a rehash of the Apple sample MapCallouts that show a single annotation, or 2. What is need for real apps is the ability to display multiple annotations from some sort of array or data structure.

Gary Benna says July 6, 2011

I have this working, thank you. However I now am working on a situation in which I have a tableview. From that I pick a cell and from that window or xib I click on a button and it flips it over to reveal a mapview. This works fine in showing the map, pin, and annotations, but I can’t get it to drop the pin, it is just there. Any suggestions on how to get this to work?

Vincent says July 28, 2011

I have problem with this error.
start in crt1.10.6.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
anyone has idea about this. please help me

Jaimin says August 4, 2011

how can i add multiple place marks on single map as just example one pin should be dropped at your current location and other pin should be dropped at other location & so on there would be other pins should be dropped by on loading map how can i create that?

Abhishek says March 17, 2012

Thanks, code is working for me…

Prasad Lodha says July 26, 2012

Very helpful tutorial. Thanks. Much appreciated.

Gautam says August 7, 2012

Thanbks Its a helpful tutorial !

iOS : zoom in and zoom out effect on map - feed99 says November 14, 2012

[…] have created map as show on this link and its working […]

Reply says December 15, 2012

[…] na sdk 4.3 Projekt z tego tutoriala MapKit example in iPhone | iPhone Tutorial | iPhone iOS4 iPad SDK Development & Programming Blog jest napisany pod sdk 4.0, zmieniam na sdk 4.3 w xcodzie i nagle wyskakuje 11 bledow (w przypadku […]

raju says January 23, 2013

Use appdelegate string as singleton app.h first pe jaega
@class first;

@interface WOTWAppDelegate : NSObject {

NSString *str1;

}@property ( nonatomic,retain) UINavigationController *nav;

@property ( nonatomic,retain) NSString *str1;

@synthesize window,nav,str1;
self.viewController = [[[first alloc] initWithNibName:@”first” bundle:nil] autorelease];
self.nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = self.nav;
[self.window makeKeyAndVisible];

andrew seth says January 23, 2013

Gridview * sat = [[Gridview alloc]init];
[self.navigationController pushViewController:sat animated:NO];


imagesList = [[NSMutableArray alloc] init];
[imagesList addObject:@”A1.png”];

2nd me
return [imagesList count];

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @”Cell”;
customcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

cell.Img.image=[UIImage imageNamed:[imagesList objectAtIndex:indexPath.row]];
cell.lbl.text = [arryData objectAtIndex:indexPath.row];
cell.sublbl.text = [subtittledata objectAtIndex:indexPath.row];
// Configure the cell…

return cell;

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

WOTWAppDelegate *app=[[UIApplication sharedApplication] delegate];
app.str1=[arryData objectAtIndex:indexPath.row] ;

WOTWViewController *st = [[WOTWViewController alloc]init];
[self.navigationController pushViewController:st animated:YES];


andrew seth says January 23, 2013

WOTWAppDelegate *app=[[UIApplication sharedApplication] delegate];

NSLog(@”namepdf= %@”,app.str1);
if ([str1 isEqualToString:@”A-Family-Man.pdf”])

Add Your Reply