Click here to Skip to main content
Click here to Skip to main content

Tagged as

iOS Mapview and Annotation Usage

, 7 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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

Using the Code

The steps we need to follow to create this sample app are:

  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.

Design Your Interface

As you see, here is drag and drop stuff:

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".

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

Add a Class For Annotation

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

License

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

Share

About the Author

Safak Tarazan
Software Developer Information Technologies
Turkey Turkey
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 7 Jul 2013
Article Copyright 2013 by Safak Tarazan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid