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/MDSMapKitDemo
Licensed Under Attribution-NonCommercial-NoDerivs 3.0
http://creativecommons.org/licenses/by-nc-nd/3.0/us/
71. THE LOCATION MODEL
CLLocation
➡ 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
Y U NO WORK!?
Invalid
84. with
CLLocationCoordinate2DIsValid(CLLocationCoordinate2D coord)
Monday, April 16, 12
85. REGION MONITORING
CLRegion
➡ 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)
➡ 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
86. 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:
122. GEOCODING
CLGeocoder
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
Monday, April 16, 12
124. RUNNING IN THE BACKGROUND
Location services are a permitted background task
Enabled in the Info.plist:
keyUIBackgroundModes/key
array
stringlocation/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
328. MAPS
MKMapView
The basic way of showing geographic data
Can show the user’s location with out the need for Core Location
mapview.showsUserLocation = YES;
[mapView setUserTrackingMode:MKUserTrackingModeFollow
animated:YES];
Monday, April 16, 12
329. ANNOTATIONS
MKAnnotation + MKAnnotationView
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.
Monday, April 16, 12
330. OVERLAYS
MKOverlay + MKOverlayView
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)zoomScale
inContext:(CGContextRef)context;
Monday, April 16, 12
334. GDAL
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
Monday, April 16, 12