Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

iOS Mapview and Annotation Usage

5.00/5 (1 vote)
7 Jul 2013CPOL2 min read 16.6K  
Creation of a very simple MapView app and putting an annotation on it

Introduction

In this tip, we will create a very simple MapView application with an annotation on it.

Background<o:p>

Objective C and Xcode experience will make this tip more understandable.

Using the Code<o:p>

The steps we need to follow to create this sample app are:<o:p>

  1. Create a new project
  2. Add MapKit Framework to your project
  3. Design your interface
  4. Add a class for annotation
  5. Finally set the coordinate of your points on the map
  6. Result

I assumed that you created a new project already, so let us start...

Add MapKit Framework to Your Project

Just click on your project and you will see the following screen, then click " + " sing and add MapKit Framework.

Image 1

Image 2

Design Your Interface

As you see, here is drag and drop stuff:

Image 3

Here, right click on your MapView and drag it your Controller header. When you release, there will a pop-up menu, write a name there for your MapView. Here it is "ampView".

Image 4

Here, same things for the button, but when the pop-up menu goes on, select the Connection as "Action" then give its name.

Image 5

Add a Class For Annotation

Image 6

After you added the class, add this code to your Annotation header file:

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

Here, there are some terms:

Nonatomic

If a variable can be accessed by more than one thread at a point of a time, it is nonatomic. It provides performance, but it is not thread safe.

Atomic

If a variable accessed by only one thread at a point of time, it is atomic and it is thread safe but it decreases the performance.

Assign

Input value is directly assigned to parameter in the setter.

Copy

Input value is copied and assigned to parameter in the setter.

Weak

When an object is created in the setter, its retain count is not increased.

Retain

When an object is created in the setter, its retain count is increased.

Do not forget to import MapKit Header file in your Annotation header file.

Do not forget to synthesize them in the Annotation main file.

@synthesize coordinate,title,subtitle; 

Finally, Set Your Coordinated of Your Point on Your Map

If we need to put an annotation on the map, we need to specify a region and set its span and center point interms of lat and lng.

MKCoordinateRegion myRegion;
CLLocationCoordinate2D myCoordinate;
myCoordinate.latitude  = 39.871738;
myCoordinate.longitude = 32.746382;
MKCoordinateSpan mySpan;
mySpan.latitudeDelta=0.30f;
mySpan.longitudeDelta = 0.30f;
myRegion.center = myCoordinate;
myRegion.span = mySpan;

So far, we create a region and set its parameters. Now, we will assign this region to our mapView.

[mapView setRegion:myRegion animated:YES];

Now, we are creating an annotation object and setting its coordinate, title and subtitle. Then, by using addAnnotation method of mapView, we add the annotation to our mapView.

Annotation *myAnnotation = [[Annotation alloc] init];  
myAnnotation.coordinate = myCoordinate;
myAnnotation.title = @"Karmasis";
myAnnotation.subtitle =@"Cyberpark C blok 13";
[mapView addAnnotation:myAnnotation]; 

If we would like to add more annotation on a map, we need to create a NSMutableArray which takes annotation and we will add this array to mapView by using addAnntations method of mapView.

Result

Image 7

Image 8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)