Blog
In this tutorial, I will demonstrate how to create a simple iPhone application that uses uses geolocation to flag multiple locations on a map using latitude and longitude coordinates. XCode has its own built-in framework called "MapKit Frameworks" which has several API that work with Google maps. I believe this application would be a good start for the beginners.
Let's create a simple view based application called 'MyMap' which has a collection of latitude and longitude coordinates stored in a dictionary object, eventually loading all the coordinates into the map view and flagging them.
Step 1: Create a new View based application titled as "MyMap".
Step 2: Right click on the Frameworks-> Add->Existing Frameworks and select MapKit.framework
Step 3: Change the header file "MyMapViewController.h" as below:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface AnnotationInfo : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
@interface MyMapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
NSMutableDictionary *locations;
}
@property (nonatomic, retain) MKMapView *mapView;
- (void) latitude:(NSString*) lat longitude:(NSString*) lon;
@end
Please ensure that you set the "MKMapViewDelegate" delegate in the "MyMapViewController" class.
Step 4: Edit "MyMapViewController.xib" using Interface Builder.

Connect IBOutlet "mapView" into the MKMapView object in the view.
Step 5: Create the class called "AnnotationInfo" for creating annotation view and change the MyMapViewController class with the below code.
#import "MyMapViewController.h"
@implementation AnnotationInfo
@synthesize coordinate,title,subtitle;
-(void)dealloc{
[title release];
[subtitle release];
[super dealloc];
}
@end
@implementation MyMapViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeStandard;
//load all the default locations needs to set annotation mark
locations = [[NSMutableDictionary alloc] init];
[locations setObject:@"38.872993,-77.368469" forKey:@"Three Pillar Global, Inc."];
[locations setObject:@"38.872392,-77.372417" forKey:@"Harris Teeter"];
[locations setObject:@"38.870922,-77.372675" forKey:@"Fair Ridge Park"];
NSArray *keys = [locations allKeys];
NSString *locs;
for (NSString *key in keys) {
locs = [[locations objectForKey:key] componentsSeparatedByString:@","];
[self latitude:[locs objectAtIndex:0] longitude:[locs objectAtIndex:1] title:key];
}
}
- (void) latitude:(NSString*) lat longitude:(NSString*) lon title:(NSString*) placeName {
MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0}};
region.center.latitude = [lat floatValue];
region.center.longitude = [lon floatValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
AnnotationInfo *annInfo = [[AnnotationInfo alloc] init];
annInfo.title = placeName;
annInfo.subtitle = [NSString stringWithFormat:@"%f, %f", mapView.region.center.latitude, mapView.region.center.longitude];
annInfo.coordinate = region.center;
[mapView setDelegate:self];
[mapView addAnnotation:annInfo];
annInfo = nil;
[annInfo release];
}
Step 6: Build the application and ensure that the annotation points are in the right place or not. The result will be shown as below:

Categories
News
Careers
Passionate, talented, ambitious, creative … sound familiar?
Three Pillar Global is hiring the best and brightest, and we might just be looking for you.
Check out our open positions.


Leave a comment