Finding Your Way Around Map Kit

33
FINDING YOUR WAY AROUND MAPKIT by Ryan Johnson MOBILE DATA SOLUTIONS @_RyanJohnson_ Monday, April 16, 12

description

The basics of Core Location and Map Kit.As presented to the MN Cocoa Heads Group for the April 2012 Meeting.Code Available Here:https://github.com/mobiledatasolutions/MDSMapKitDemoLicensed Under Attribution-NonCommercial-NoDerivs 3.0http://creativecommons.org/licenses/by-nc-nd/3.0/us/

Transcript of Finding Your Way Around Map Kit

Page 1: Finding Your Way Around Map Kit

FINDING YOUR WAY AROUND MAPKIT

byRyan����������� ������������������  Johnson

MOBILE DATA SOLUTIONS@_RyanJohnson_Monday, April 16, 12

Page 2: Finding Your Way Around Map Kit

WHAT WE’LL COVER

Core Location

The Basics

Simulating Location

Geocoding

Map Kit

The Basics

Annotations

Overlays

Advanced Drawing & Animation

Monday, April 16, 12

Page 3: Finding Your Way Around Map Kit

CORE LOCATIONTHE����������� ������������������  BACKEND

Monday, April 16, 12

Page 4: Finding Your Way Around Map Kit

GETTING THE USER’S LOCATIONCLLocationManager

Monday, April 16, 12

Page 5: Finding Your Way Around Map Kit

GETTING THE USER’S LOCATION

1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];

CLLocationManager

Monday, April 16, 12

Page 6: Finding Your Way Around Map Kit

GETTING THE USER’S LOCATION

1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];

2. Set a delegate: manager.delegate = (id<CLLocationManagerDelegate>)self;

CLLocationManager

Monday, April 16, 12

Page 7: Finding Your Way Around Map Kit

GETTING THE USER’S LOCATION

1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];

2. Set a delegate: manager.delegate = (id<CLLocationManagerDelegate>)self;

3. Set your desired accuracy & start monitoring:manager.desiredAccuracy = kCLLocationAccuracyBest;[manager startUpdatingLocation];

CLLocationManager

Monday, April 16, 12

Page 8: Finding Your Way Around Map Kit

GETTING THE USER’S LOCATION

1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];

2. Set a delegate: manager.delegate = (id<CLLocationManagerDelegate>)self;

3. Set your desired accuracy & start monitoring:manager.desiredAccuracy = kCLLocationAccuracyBest;[manager startUpdatingLocation];

4. Location updates are sent to the delegate method:- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

CLLocationManager

Monday, April 16, 12

Page 9: Finding Your Way Around Map Kit

GETTING THE USER’S LOCATION

1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];

2. Set a delegate: manager.delegate = (id<CLLocationManagerDelegate>)self;

3. Set your desired accuracy & start monitoring:manager.desiredAccuracy = kCLLocationAccuracyBest;[manager startUpdatingLocation];

4. Location updates are sent to the delegate method:- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

CLLocationManager

PROTiP:����������� ������������������  GPS����������� ������������������  hates����������� ������������������  batteries.����������� ������������������  You����������� ������������������  can����������� ������������������  help����������� ������������������  save����������� ������������������  the����������� ������������������  user’s����������� ������������������  battery����������� ������������������  by����������� ������������������  setting����������� ������������������  the����������� ������������������  desired����������� ������������������  accuracy����������� ������������������  to����������� ������������������  what����������� ������������������  you����������� ������������������  actually����������� ������������������  need.����������� ������������������  Less����������� ������������������  accurate����������� ������������������  =����������� ������������������  more����������� ������������������  battery����������� ������������������  life����������� ������������������  =����������� ������������������  better����������� ������������������  UX.

manager.desiredAccuracy = kCLLocationAccuracyThreeKilometers

Monday, April 16, 12

Page 10: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #1:The����������� ������������������  World����������� ������������������  Is����������� ������������������  Round.

Latitude����������� ������������������  =����������� ������������������  the����������� ������������������  east/west

Longitude����������� ������������������  =����������� ������������������  the����������� ������������������  north/south

The����������� ������������������  distance����������� ������������������  or����������� ������������������  direction����������� ������������������  between����������� ������������������  two����������� ������������������  points����������� ������������������  is����������� ������������������  calculated����������� ������������������  over����������� ������������������  the����������� ������������������  sphere����������� ������������������  of����������� ������������������  the����������� ������������������  earth����������� ������������������  not����������� ������������������  a����������� ������������������  straight����������� ������������������  line.

Monday, April 16, 12

Page 11: Finding Your Way Around Map Kit

➡ location.coordinate - the CLLocationCoordinate2D struct with the latitude and longitude

➡ location.horizontalAccuracy - the accuracy of the coordinate in meters

➡ location.speed - the user’s speed in meters per second

➡ location.course - the direction the user is headed in degrees

THE LOCATION MODELCLLocation

Y U NO WORK!?Invalid����������� ������������������  Coordinates����������� ������������������  can����������� ������������������  cause����������� ������������������  tough����������� ������������������  to����������� ������������������  debug����������� ������������������  issues.����������� ������������������  

You����������� ������������������  can����������� ������������������  validate����������� ������������������  your����������� ������������������  coordinates����������� ������������������  withCLLocationCoordinate2DIsValid(CLLocationCoordinate2D coord)

Monday, April 16, 12

Page 12: Finding Your Way Around Map Kit

REGION MONITORING

➡ Easy to use geofencing - lets you know when a user enters or exits a defined circular geographic region.

➡ Can continue to run in the background, relaunching your app when a user enters/exits a region.

➡ Shared system resource (limit, identifiers)

CLRegion

➡ Create a CLRegion to be monitored:CLLocationCoordinate2D regionCenter = {42.443904, -71.122044};CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter:regionCenter radius:1000 identifier:@"A Monitored Region"];

➡ Send the CLRegion to a CLLocationManager to be monitored[_locationManager startMonitoringForRegion:region desiredAccuracy:50];

➡ Region events are passed to the CLLocationManager delegate’s methods:- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region; - (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region;

Monday, April 16, 12

Page 13: Finding Your Way Around Map Kit

SIMULATING YOUR LOCATION

➡ Can simulate a pin point location or travel a long a series of points

➡ If you do nothing, the simulator’s default location is in San Francisco.

➡ Three Ways to Simulate Your Location

➡ Default locations in Xcode

➡ A GPX File

➡ UIAutomation - UIATarget setLocation(coordinateDictionary)

PROTIP:����������� ������������������  Only����������� ������������������  simulate����������� ������������������  your����������� ������������������  location����������� ������������������  on����������� ������������������  devices����������� ������������������  dedicated����������� ������������������  to����������� ������������������  development.����������� ������������������  Your����������� ������������������  simulated����������� ������������������  location����������� ������������������  is����������� ������������������  reported����������� ������������������  to����������� ������������������  all����������� ������������������  apps.����������� ������������������  So,����������� ������������������  Maps����������� ������������������  or����������� ������������������  Path����������� ������������������  will����������� ������������������  think����������� ������������������  you’re����������� ������������������  in����������� ������������������  Pyongyang����������� ������������������  when����������� ������������������  you’re����������� ������������������  really����������� ������������������  in����������� ������������������  Eagan.

Monday, April 16, 12

Page 14: Finding Your Way Around Map Kit

CODE DEMOBASICS����������� ������������������  AND����������� ������������������  

LOCATION����������� ������������������  SIMULATION

Monday, April 16, 12

Page 15: Finding Your Way Around Map Kit

Reverse geocoding introduced in iOS 3 with MKReverseGeocoder (now depreciated)

iOS 5 now allows for forward and reverse geocoding in the new CLGeocoder class.

Forward Geocoding - turns an address into a coordinate

Reverse Geocoding - turns a coordinate into an address

GEOCODINGCLGeocoder

Monday, April 16, 12

Page 16: Finding Your Way Around Map Kit

CODE DEMOGEOCODING

Monday, April 16, 12

Page 17: Finding Your Way Around Map Kit

RUNNING IN THE BACKGROUND

Location services are a permitted background task

Enabled in the Info.plist:<key>UIBackgroundModes</key><array><string>location</string></array>

Region Monitoring does not require the background task permission. Your app will be relaunched in the background to allow for processing the region event.

Monday, April 16, 12

Page 18: Finding Your Way Around Map Kit

MAP KITTHE����������� ������������������  FRONT����������� ������������������  END

Monday, April 16, 12

Page 19: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

Monday, April 16, 12

Page 20: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

Monday, April 16, 12

Page 21: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

MKMapView����������� ������������������  uses����������� ������������������  a����������� ������������������  web����������� ������������������  mercator����������� ������������������  projection.

Monday, April 16, 12

Page 22: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

MKMapView����������� ������������������  uses����������� ������������������  a����������� ������������������  web����������� ������������������  mercator����������� ������������������  projection.

Monday, April 16, 12

Page 23: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

MKMapView����������� ������������������  uses����������� ������������������  a����������� ������������������  web����������� ������������������  mercator����������� ������������������  projection.

Monday, April 16, 12

Page 24: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

MKMapView����������� ������������������  uses����������� ������������������  a����������� ������������������  web����������� ������������������  mercator����������� ������������������  projection.

Monday, April 16, 12

Page 25: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

MKMapView����������� ������������������  uses����������� ������������������  a����������� ������������������  web����������� ������������������  mercator����������� ������������������  projection.

Monday, April 16, 12

Page 26: Finding Your Way Around Map Kit

Geography����������� ������������������  Lesson����������� ������������������  #2:Maps����������� ������������������  are����������� ������������������  flat.����������� ������������������  this����������� ������������������  is����������� ������������������  a����������� ������������������  problem.

There����������� ������������������  are����������� ������������������  a����������� ������������������  lot����������� ������������������  of����������� ������������������  ways����������� ������������������  to����������� ������������������  make����������� ������������������  a����������� ������������������  circle����������� ������������������  flat.����������� ������������������  

MKMapView����������� ������������������  uses����������� ������������������  a����������� ������������������  web����������� ������������������  mercator����������� ������������������  projection.

Mapkit����������� ������������������  uses����������� ������������������  its����������� ������������������  own����������� ������������������  structures����������� ������������������  (MKMapPoint,����������� ������������������  MKMapRect)����������� ������������������  to����������� ������������������  handle����������� ������������������  this����������� ������������������  conversion

Monday, April 16, 12

Page 27: Finding Your Way Around Map Kit

The basic way of showing geographic data

Can show the user’s location with out the need for Core Locationmapview.showsUserLocation = YES;[mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

MAPSMKMapView

Monday, April 16, 12

Page 28: Finding Your Way Around Map Kit

The basic way of presenting point data on a map

Comes with a disclosure pop-out for additional detail

MKAnnotation is the basic model (really a protocol) and MKAnnotationView is the view. You add the MKAnnotation to the map, and the map is provided a view by the map’s delegate.

ANNOTATIONSMKAnnotation + MKAnnotationView

Monday, April 16, 12

Page 29: Finding Your Way Around Map Kit

Allows you to place paths or custom drawing over a map.

Provided basic path types: MKCircle, MKPolyline, MKPolygon

Can create your own view classes to easily draw over a map.

Custom drawing must be done by overriding:- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScaleinContext:(CGContextRef)context;

OVERLAYSMKOverlay + MKOverlayView

Monday, April 16, 12

Page 30: Finding Your Way Around Map Kit

CODE DEMOANNOTATIONS����������� ������������������  +����������� ������������������  OVERLAYS

Monday, April 16, 12

Page 31: Finding Your Way Around Map Kit

Geospatial Data Abstraction Library

www.gdal.com or brew install gdal

A command line tool to create your own tile images and to convert the projection of images

Essential if you’re going to put your own images on a map

GDAL

Monday, April 16, 12

Page 32: Finding Your Way Around Map Kit

CODE DEMOTILED����������� ������������������  IMAGES

Monday, April 16, 12

Page 33: Finding Your Way Around Map Kit

WWDC Videos & Sample Code -2010 Session - Customizing Maps With Overlays KML https://developer.apple.com/library/ios/samplecode/KMLViewer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010046

Tiled Images (in the Code Bundle) http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?

wosid=aY5boO0HQiIJ3STghf12IbXXhCt&fileID=26713&code=y&source=x

2011 Session - Visualizing Information Graphically With MapKit

Open Source Code -VPPMap (Annotation Clustering)- https://github.com/vicpenap/VPPMapCore Location Utils - https://github.com/100grams/CoreLocationUtils

ADDITIONAL RESOURCES

Monday, April 16, 12