MapView and Annotation in iOS

In Xcode 6.3, go to File\New\New Project, select iOS\Application\Single View Application, and click Next.

Add New Project

On the Choose option for your new project screen,  Fill the all information about your project and click Next.

Choose your project

Save Your Project.

Click on Main.storyboard to bring up Interface Builder.


From the Object library, Select MapKit View and drag to the screen and fit as you want.

Mapkit View

Add the MapKit framework to your project, In Xcode 6.3, click on the name of your project, in General Tab, Go bottom of the screen. Locate Linked Frameworks and Libraries, click + button below.

Locate Framework and Libraries

In the “Choose Framework and Libraries” popup, typeMapKit, Select MapKit.Framework and click on add.

Add MapKit Framework

Click on Assistant Editor Button, then click on the Map Screen and hold control key and drag it to .h file and Set IBoutlet.

Assistant Editor


Go to ViewController.h file and import <MapKit/MapKit.h>

Import MapKit

Run the project. You will see map on the device.

Run on Simulator

Open ViewController.m, and add the following underneath the #imports and before the @implementation:

#define METERS_PER_MILE 1609.344


Now synthesize the mapView in ViewController.m file.

@synthesize mapView;

Now implement viewWillAppear ,

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 28.632464;
    zoomLocation.longitude= 77.221023;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    [mapView setRegion:viewRegion animated:YES];

Run your Project again, you will find your entered location on map with zoom.

Map Run on simulator

Now Drop Pin on the Map

Add the below code in the viewWillAppear Method.

MKPointAnnotation *point= [[MKPointAnnotation alloc]init];
point.coordinate = zoomLocation;
point.title = @"Where i am?";
point.subtitle = @"I am Here!!";
[self.mapView addAnnotation:point];

Run the Project, you will find pin on the map.

Map Annotation in iOS

That’s It.

If you like this post, Please comment below or subscribe this blog.

Leave your comments


How to pass data back to previous ViewController using PresentViewController

Let suppose, We have to pass data from B to A ViewController. Here, I am using Present View Controller. When you  dismiss PresentViewController, Data will back to Parent View Controller.

In the B viewController.h File,

@protocol senddata <NSObject>
-(void) Passdatatoback:(NSString*)Passdataback;

Also set the property in .h file,

@property(nonatomic, assign)id delegate;

Now, move to B viewController.m File,

@synthesize delegate;

and set a method,

[delegate Passdatatoback:@”This is data which you want in A ViewController”];

Now, Move to A viewController,

Create a same method which you have create in B ViewController.m file.

-(void) Passdatatoback:(NSString*)Passdataback{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Your string Data Showing” message:Passdataback delegate:self cancelButtonTitle:@”OK “ otherButtonTitles:nil];
 [alert show];

In A ViewContrller.m file, When You go in B ViewController,

BViewConoller *BVC = [[BViewConoller alloc]init];
BVC.delegate= self;
[self.navigationController presentViewController:BVC animated:YES completion:nil];

That’s It.

If you like this post, Please comment below or subscribe this blog.

Leave your comments