Introduction to MapKit

Christopher M. Judd

Wednesday, April 27, 2011

Christopher M. Judd
President/Consultant of leader Columbus Developer User Group (CIDUG)

Wednesday, April 27, 2011

Remarkable Ohio

Free
Developed for eTech Ohio and Ohio Historical Center
Wednesday, April 27, 2011

University System Of Ohio

Developed for eTech Ohio and University System Of Ohio
Wednesday, April 27, 2011

Free

Your mobile phone has more computing power than all of NASA in 1969. NASA launched a man to the moon.

Wednesday, April 27, 2011

We launch a bird into pigs.
twitter
Wednesday, April 27, 2011

MapKit Basics

Wednesday, April 27, 2011

MapKit

MKMapView

Wednesday, April 27, 2011

Current Location

Core Location Framework
Wednesday, April 27, 2011

Current Location

Core Location Framework
Wednesday, April 27, 2011

Current Location

Core Location Framework
Wednesday, April 27, 2011

Current Location

Core Location Framework
Wednesday, April 27, 2011

Current Location
1 kilometer

Core Location Framework
Wednesday, April 27, 2011

Current Location
1 kilometer

100 meters

Core Location Framework
Wednesday, April 27, 2011

Current Location
1 kilometer

100 meters

10 meters

Core Location Framework
Wednesday, April 27, 2011

Map Types

Wednesday, April 27, 2011

Map Types

Standard
Wednesday, April 27, 2011

Map Types

Standard
Wednesday, April 27, 2011

Satellite

Map Types

Standard
Wednesday, April 27, 2011

Satellite

Hybrid

Annotations

Wednesday, April 27, 2011

Classes and Protocols

Wednesday, April 27, 2011

Adding Maps

Wednesday, April 27, 2011

Adding Maps Steps
1. Add MapView component to view 2. Add MapKit Framework to target libraries 3. Run Application

Wednesday, April 27, 2011

Add Map View Component

Wednesday, April 27, 2011

Add MapKit Framework

Wednesday, April 27, 2011

Run Application

Wednesday, April 27, 2011

Display Current Location

Wednesday, April 27, 2011

Show User Location

Wednesday, April 27, 2011

Simulator Location

Wednesday, April 27, 2011

iSimulate

$15.99 in App Store
Wednesday, April 27, 2011

Change Map Types

Wednesday, April 27, 2011

Map

Satellite

Hybrid
Wednesday, April 27, 2011

switch (((UISegmentedControl *)sender).selectedSegmentIndex) { case 0: { mapView.mapType = MKMapTypeStandard; break; } case 1: { mapView.mapType = MKMapTypeSatellite; break; } default: { mapView.mapType = MKMapTypeHybrid; break; } }

Map

Satellite

Hybrid
Wednesday, April 27, 2011

Adding Annotations

Wednesday, April 27, 2011

Basic Custom Annotation
HistoryMarker.h
Extend MKAnnotation @interface HistoryMarker : NSObject <MKAnnotation> { CLLocationCoordinate2D _coordinate; Implement required coordinate property } @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate; @end

HistoryMarker.m
@implementation HistoryMarker @synthesize coordinate = _coordinate; } (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate { if((self = [super init])) { _coordinate = coordinate; } return self;

@end
Wednesday, April 27, 2011

Add Annotation
*Controller.m
- (void)viewDidLoad { [super viewDidLoad]; CLLocationCoordinate2D coordinate = {39.960307, -82.98685}; HistoryMarker* marker = [[HistoryMarker alloc] initWithCoordinate:coordinate]; [_mapView addAnnotation:marker]; }

Wednesday, April 27, 2011

Positioning Map

Wednesday, April 27, 2011

Multiple Pins Can be hard to see from far away

Applications with a geographical focus should frame the region
Wednesday, April 27, 2011

Zooming

- (IBAction)zoomColumbus:(id)sender { CLLocationCoordinate2D columbusCenterCoordinate = {39.971793, -82.983396}; MKCoordinateSpan span = MKCoordinateSpanMake( 0.390456, 0.390456); MKCoordinateRegion region = MKCoordinateRegionMake(columbusCenterCoordinate, span); [_mapView setRegion:region animated:TRUE]; }

- (IBAction)zoomCurrentLocation:(id)sender { MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance( _mapView.userLocation.location.coordinate, 500, 500); [_mapView setRegion:region animated:TRUE]; }
Wednesday, April 27, 2011

Decorating Annotations

Wednesday, April 27, 2011

Changing Annotation Appearance
*Controller.h
Implement MKMapViewDelegate protocol
@interface ColumbusHistoryMapViewController : UIViewController <MKMapViewDelegate> { IBOutlet MKMapView* _mapView; } @property (nonatomic, retain) MKMapView* mapView; @end

*Controller.m
@implementation ColumbusHistoryMapViewController - (void)viewDidLoad { Assign delegate [super viewDidLoad]; _mapView.delegate = self;
// Details removed for brevity

Implement viewForAnnotation

} - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if(annotation == mapView.userLocation) { return nil; } // Details removed for brevity } return annotationView;

Returning nil will perform default behavior

// Details removed for brevity

@end
Wednesday, April 27, 2011

Coloring Pins
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if(annotation == mapView.userLocation) { return nil; } MKPinAnnotationView *annotationView = nil; annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"historyMarker"]; if(nil == annotationView) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"historyMarker"]; } annotationView.pinColor = MKPinAnnotationColorPurple; return annotationView; }

Limited to 3 colors
MKPinAnnotationColorPurple MKPinAnnotationColorRed MKPinAnnotationColorGreen

Wednesday, April 27, 2011

Annotation Images
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if(annotation == mapView.userLocation) { return nil; } MKAnnotationView *annotationView = nil; annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"historyMarker"]; if(nil == annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"historyMarker"]; } annotationView.image = [UIImage imageNamed:@"history.png"]; return annotationView; }

Wednesday, April 27, 2011

Free Map Icons
http://code.google.com/p/google-maps-icons/

Wednesday, April 27, 2011

Be Creative
Wednesday, April 27, 2011

Callouts

Wednesday, April 27, 2011

Enable Callouts
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if(annotation == mapView.userLocation) { return nil; } MKAnnotationView *annotationView = nil; annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"historyMarker"]; if(nil == annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"historyMarker"]; } annotationView.image = [UIImage imageNamed:@"history.png"]; annotationView.canShowCallout = YES; return annotationView; Set canShowCallout }

Implement title and optionally subtitle on MKAnnotation Implementation
@implementation HistoryMarker - (NSString *)title { return _name; } - (NSString *)subtitle { return [NSString stringWithFormat:@"%f, %f", _coordinate.latitude, _coordinate.longitude]; } @end

Wednesday, April 27, 2011

Add Behavior to Callouts

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { if(annotation == mapView.userLocation) { return nil; } MKAnnotationView *annotationView = nil; annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"historyMarker"]; if(nil == annotationView) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"historyMarker"]; } annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; annotationView.image = [UIImage imageNamed:@"history.png"]; annotationView.canShowCallout = YES; return annotationView; }

Implement calloutAccessoryControlTapped

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { HistoryMarker* historyMarker = (HistoryMarker*)view.annotation; NSLog(@"Historical Marker %@ was selected.", historyMarker.name); //Could open a detail view, navigate to a website, call, etc. }

Wednesday, April 27, 2011

Directions

Wednesday, April 27, 2011

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { HistoryMarker* historyMarker = (HistoryMarker*)view.annotation; NSString* url = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", historyMarker.coordinate.latitude, historyMarker.coordinate.longitude, _mapView.userLocation.location.coordinate.latitude, _mapView.userLocation.location.coordinate.longitude]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; }

http://mapki.com/index.php?title=Google_Map_Parameters
Wednesday, April 27, 2011

Alternatives

Wednesday, April 27, 2011

New Free BSD

http://code.google.com/p/route-me/
Wednesday, April 27, 2011

http://developers.cloudmade.com/projects/show/iphone-api
Wednesday, April 27, 2011

Wednesday, April 27, 2011

Resources

The Objective-C Programming Language

Wednesday, April 27, 2011

Columbus History Map Source

https://github.com/cjudd/ColumbusHistoryMap_iOS
Wednesday, April 27, 2011

Christopher M. Judd
President/Consultant/Author email: cjudd@juddsolutions.com web: www.juddsolutions.com blog: juddsolutions.blogspot.com twitter: javajudd

Wednesday, April 27, 2011

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.