iPhone SDK 3.0 - Playing with Map Kit - Part 3

This is a follow up post to my 2 previous posts on the same subject. Please check them here.

iPhone SDK 3.0 - Playing with Map Kit iPhone SDK 3.0 - Playing with Map Kit - Part 2

We also started a basics training program in iPhone development. If you or some one you know wants to learn development and live around NYC area, Please promote this

Blog Entry:http://blog.objectgraph.com/index.php/2009/05/13/iphone-developer-training-in-ny-city/ Meetup Group: http://www.meetup.com/iPhoneDeveloper

Many of the users were asking me how to draw a polyline on this map. From what i can see in the documentation as of today, there are no classes that provide this yet.

I am using this as a source of documentation. This might change in the near future as there is still time before the official release of the iPhone 3.0


You can draw something on the view using Quartz but I think this is not as useful as drawing directly on the map.

Currently you can do the following very easily as i explained in my previous posts.

So just by using this limited API you could create different kinds of applications.

I found out the best way to get a users location is by using CoreLocation as it provides a nice delegate to let me know when the coordinates are obtained from the GPS asynchronously.

So I was able to combine the MapKit and the Core Location API to zoom into your current location and place an Annotaion.

Here is some of the code.

Header File

#import "FlipsideViewController.h"
#import "ParkPlaceMark.h"

@interface MainViewController : UIViewController  {
	MKMapView *mapView;
	MKPlacemark *mPlacemark;
	CLLocationCoordinate2D location;
	IBOutlet UIButton *mStoreLocationButton;

- (IBAction)showInfo;
- (IBAction)storeLocationInfo:(id) sender;


Code File

#import "MainViewController.h"
#import "MainView.h"

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 return self;

 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
 [super viewDidLoad];
	 mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
	 [self.view insertSubview:mapView atIndex:0];
	 CLLocationManager *locationManager=[[CLLocationManager alloc] init];
	 [locationManager startUpdatingLocation];

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
	[self dismissModalViewControllerAnimated:YES];

- (IBAction)showInfo { 
	FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
	controller.delegate = self;
	controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:controller animated:YES];
	[controller release];

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn"t have a superview.
 [super didReceiveMemoryWarning];
	// Release any cached data, images, etc that aren"t in use.

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;

- (void)dealloc {
 [super dealloc];

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
	NSLog(@"Geocoder completed");
	[mapView addAnnotation:placemark];

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{
	NSLog(@"This is called");
	MKPinAnnotationView *test=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"parkingloc"];
	if([annotation title]==@"Parked Location")
		[test setPinColor:MKPinAnnotationColorPurple];
		[test setPinColor:MKPinAnnotationColorGreen];
	return test;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
	//One location is obtained.. just zoom to that location
	MKCoordinateRegion region;
	//Set Zoom level using Span
	MKCoordinateSpan span;
	[mapView setRegion:region animated:TRUE];


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

- (IBAction)storeLocationInfo:(id) sender{
	//Either we can use geocoder to get a placemark or just create our own.
	MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
	[geocoder start];
	//Our own
	ParkPlaceMark *placemark=[[ParkPlaceMark alloc] initWithCoordinate:location];
	[mapView addAnnotation:placemark];


Also you could use a reverse Geocoder to obtain a placemark or create your own annotation. Here is some code on how to quickly create your own annotation by implementing the MKAnnotation protocol.

Header File


@interface ParkPlaceMark : NSObject {
	CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;



Source File

#import "ParkPlaceMark.h"

@implementation ParkPlaceMark
@synthesize coordinate;

- (NSString *)subtitle{
	return @"Put some text here";
- (NSString *)title{
	return @"Parked Location";

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
	return self;

Download the Files

By: gavi on: