iPhone SDK 3.0 - Playing with Map Kit - Part 2

I got some time to play with some more Map Kit stuff today. And I was able to add annotations.

UPDATE: Follow the 3rd part here.http://blog.objectgraph.com/index.php/2009/04/08/iphone-sdk-30-playing-with-map-kit-part-3/

There are a couple of classes to be familiar with

MKMapView

This is our main class that displays a map. It inherits from UIView, so you can just initialize it and add it to your MainView

MKPinAnnotationView

This view is helpful if you would not like to subclass the MKAnnotationView, It provides a basic pin annotation with interaction enabled

MKReverseGeoCoder

This class provides a placemark via a callback asynchronously given a coordinate

MKPlacemark

A representation of an annotation

Also some delegates are useful

MKMapViewDelegate

This is an important one as whenever you add an annotation to the map, there is a callback method you need to implement to show the View of the annotation.

MKReverseGeocoderDelegate

Used in conjunction with MKReverseGeoCoder to obtain placemarks

Here is my code for the MainViewController.h


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

@interface MainViewController : UIViewController  {
	MKMapView *mapView;
	MKReverseGeocoder *geoCoder;
	MKPlacemark *mPlacemark;
	IBOutlet UISegmentedControl *mapType;
}
- (IBAction)changeType:(id) sender;
@end


My MainViewController.m code is below


#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.bounds];
	 mapView.showsUserLocation=TRUE;
	 mapView.mapType=MKMapTypeStandard;
	 mapView.delegate=self;
	 
	 /*Region and Zoom*/
	 MKCoordinateRegion region;
	 MKCoordinateSpan span;
	 span.latitudeDelta=0.2;
	 span.longitudeDelta=0.2;
	 
	 CLLocationCoordinate2D location=mapView.userLocation.coordinate;
	 
	 location.latitude=40.814849;
	 location.longitude=-73.622732;
	 region.span=span;
	 region.center=location;
	 
	 /*Geocoder Stuff*/
	 
	 geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
	 geoCoder.delegate=self;
	 [geoCoder start];
	 
	 
	 [mapView setRegion:region animated:TRUE];
	 [mapView regionThatFits:region];
	 [self.view insertSubview:mapView atIndex:0];
 }

- (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];
}

- (IBAction)changeType:(id)sender{
	if(mapType.selectedSegmentIndex==0){
		mapView.mapType=MKMapTypeStandard;
	}
	else if (mapType.selectedSegmentIndex==1){
		mapView.mapType=MKMapTypeSatellite;
	}
	else if (mapType.selectedSegmentIndex==2){
		mapView.mapType=MKMapTypeHybrid;
	}
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
	NSLog(@"Reverse Geocoder Errored");
	
}

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

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation{
	MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
	annView.animatesDrop=TRUE;
	return annView;
}

@end


By: gavi on: