Finding Your Way Around Map Kit
-
Upload
mobiledatasolutions -
Category
Documents
-
view
2.814 -
download
5
description
Transcript of Finding Your Way Around Map Kit
FINDING YOUR WAY AROUND MAPKIT
byRyan����������� ������������������ Johnson
MOBILE DATA SOLUTIONS@_RyanJohnson_Monday, April 16, 12
WHAT WE’LL COVER
Core Location
The Basics
Simulating Location
Geocoding
Map Kit
The Basics
Annotations
Overlays
Advanced Drawing & Animation
Monday, April 16, 12
CORE LOCATIONTHE����������� ������������������ BACKEND
Monday, April 16, 12
GETTING THE USER’S LOCATIONCLLocationManager
Monday, April 16, 12
GETTING THE USER’S LOCATION
1. Instantiate a CLLocationManager object: manager = [[CLLocationManager alloc] init];
CLLocationManager
Monday, April 16, 12
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
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
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
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
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
➡ 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
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
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
CODE DEMOBASICS����������� ������������������ AND����������� ������������������
LOCATION����������� ������������������ SIMULATION
Monday, April 16, 12
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
CODE DEMOGEOCODING
Monday, April 16, 12
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
MAP KITTHE����������� ������������������ FRONT����������� ������������������ END
Monday, April 16, 12
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
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
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
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
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
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
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
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
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
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
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
CODE DEMOANNOTATIONS����������� ������������������ +����������� ������������������ OVERLAYS
Monday, April 16, 12
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
CODE DEMOTILED����������� ������������������ IMAGES
Monday, April 16, 12
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