280 likes | 293 Views
Learn about Core Location Framework, obtaining location information, CLLocation Manager, CLLocation, CLLocation Manager Delegate, CLLocation Manager Delegate protocol, and using MapKit for map display.
E N D
EEC-492/693/793iPhone Application Development Lecture 17 Wenbing Zhao & Nigamanth Sridhar EEC492/693/793 - iPhone Application Development
Outline Maps and Locations Assignment: Build the set of apps in the handout Note: Please do not use “Maps” as the name for the maps app if you want to test it on an i-device, because doing so would overwrite the build-in Maps app!!! 1/4/2020 2 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development
Core Location Framework • Classes • CLLocationManager • CLLocation • CLHeading • Protocol • CLLocationManagerDelegate • No UI EEC492/693/793 - iPhone Application Development
How to Obtain Location Information • Three tiered approach • GPS • Wifi • Cell network EEC492/693/793 - iPhone Application Development
CLLocation • An object to represent a point and vector in the real world @property CLLocationCoordinate2D coordinate; @property CLLocationDistance altitude; @property CLLocationAccuracy horizontalAccuracy; @property CLLocationAccuracy verticalAccuracy; @property CLLocationDirection course; @property CLLocationSpeed speed; - (NSDate *)timeStamp; - (CLLocationDistance)distanceFromLocation:(CLLocation *)location EEC492/693/793 - iPhone Application Development
CLLocationManager • Your entry point to the location service @property CLLocation *location; @property id <CLLocationManagerDelegate> delegate; @property CLLocationDistance distanceFilter; @property CLLocationAccuracy verticalAccuracy; - (void)startUpdatingLocation - (void)stopUpdatingLocation - (void)startUpdatingHeading - (void)stopUpdatingHeading EEC492/693/793 - iPhone Application Development
Core Location Framework:CLLocationManagerDelegate protocol • Callbacks for location change - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; • Callbacks for heading change - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading; • Error handling - (void)locationManager:(CLLocationManager *)manager didFailLoadWithError:(NSError *)error; EEC492/693/793 - iPhone Application Development
Getting a Location:Starting the location service CLLocationManager* locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; [locManager startUpdatingLocation]; EEC492/693/793 - iPhone Application Development
Getting a Location:Using the event data - (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation { NSTimeInterval howRecent = [newLocation.timestamp timeIntervalSinceNow]; if (howRecent < -10) return; if (newLocation.horizontalAccuracy > 100) return; // Use the coordinate data. double lat = newLocation.coordinate.latitude; double lon = newLocation.coordinate.longitude; } EEC492/693/793 - iPhone Application Development
Getting a Location:Using the event data - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { // Use the coordinate data CLLocationDirection heading = newHeading.trueHeading; } EEC492/693/793 - iPhone Application Development
Desired Accuracy:Choosing an Appropriate Accuracy Level • Choose an appropriate accuracy level • Higher accuracy impacts power consumption • Lower accuracy is “good enough” in most cases • Can change accuracy setting later if needed • Actual accuracy reported in CLLocation object CLLocationManager* locManager = [[CLLocationManager alloc] init]; locManager.desiredAccuracy = kCLLocationAccuracyBest; EEC492/693/793 - iPhone Application Development
Distance FilterChoosing an appropriate update threshold • New events delivered when threshold exceeded CLLocationManager* locManager = [[CLLocationManager alloc] init]; locManager.distanceFilter = 3000; // in meters EEC492/693/793 - iPhone Application Development
Stopping the Service CLLocationManager* locManager = [[CLLocationManager alloc] init]; [locManager startUpdatingLocation]; ... [locManager stopUpdatingLocation]; EEC492/693/793 - iPhone Application Development
Responding to ErrorsUser may deny use of the location service • Results in a kCLErrorDenied error • Protects user privacy • Occurs on a per-application basis EEC492/693/793 - iPhone Application Development
Responding to ErrorsLocation may be unavailable • Results in a kCLErrorLocationUnknown error • Likely just temporary • Scan continues in background EEC492/693/793 - iPhone Application Development
MapKit • API to display Maps • Classes to translate between CLLocation and human-readable addresses • Support for “annotations” (pins on a map) • Reverse Geocoding EEC492/693/793 - iPhone Application Development
MKMapView • Handles display of map • “Map” & “Satellite” types • Panning and Zooming • Annotations • Display User Location EEC492/693/793 - iPhone Application Development
MKMapView • Properties in MKMapView @property MKCoordinateRegion region; @property CLLocationCoordinate2D centerCoordinate; @property MKMapType mapType; @property NSArray *annotations; @property MKUserLocation userLocation; @property id <MKMapViewDelegate> delegate; EEC492/693/793 - iPhone Application Development
MKMapViewDelegate • Callback methods about loading state: - (void)mapViewWillStartLoadingMap:(MKMapView *)mapView; - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView; - (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error; • Callback methods about region changes: - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated; - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated; EEC492/693/793 - iPhone Application Development
MKMapViewDelegate • Callback methods to customize and interact with “annotations”: // return the view for the annotation - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation; // one or more annotation views were added to the map - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views; // the user tapped one of the annotation view’s accessory buttons - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; EEC492/693/793 - iPhone Application Development
Map Kit Data Types • MKCoordinateSpan: defines the area spanned by a map region typedef struct { CLLocationDegrees latitudeDelta; CLLocationDegrees longitudeDelta; } MKCoordinateSpan; • latitudeDelta • The amount of north-to-south distance (measured in degrees) to display on the map. One degree of latitude is approximately 111 kilometers (69 miles) • longitudeDelta • The amount of east-to-west distance (measured in degrees) to display for the map region. The number of kilometers spanned by a longitude range varies based on the current latitude. • For example, one degree of longitude spans a distance of approximately 111 kilometers (69 miles) at the equator but shrinks to 0 kilometers at the poles EEC492/693/793 - iPhone Application Development
Map Kit Data Types • MKCoordinateRegion: defines which portion of the map to display typedef struct { CLLocationCoordinate2D center; MKCoordinateSpan span; } MKCoordinateRegion; • center • The center point of the region • span • The horizontal and vertical span representing the amount of map to display • The span also defines the current zoom level used by the map view object EEC492/693/793 - iPhone Application Development
MKAnnotation • MKAnnotation is a @protocol - not a @class • Add to a MapView to plot pins @property CLLocationCoordinate2D coordinate; @property NSString *title; @property NSString *subtitle; EEC492/693/793 - iPhone Application Development
MKAnnotationView • View for displaying a “callout” above a map pin -(void)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier; @property UIImage *image; @property UIView *leftCalloutAccessoryView; @property UIView *leftCalloutAccessoryView; EEC492/693/793 - iPhone Application Development
MKPinAnnotationView • A subclass of MKAnnotationView • Displays a pin icon • @property BOOL animatesDrop • @property MKPinAnnotationColor pinColor enum { MKPinAnnotationColorRed = 0, MKPinAnnotationColorGreen, MKPinAnnotationColorPurple }; typedef NSUInteger MKPinAnnotationColor; EEC492/693/793 - iPhone Application Development
MKPlacemark • Conforms to MKAnnotation protocol • Convenience for holding human-readable addresses alongside Coordinate - (void)initWithCoordinate:(CLLocationCoordinate2D *)coordinate addressDictionary:(NSDictionary *)dictionary; • Easy to convert between AddressBook addresses and location: • thoroughfare, subThoroughfare, locality, subLocality, administrativeArea, subAdministrativeArea, postalCode, country, countryCode EEC492/693/793 - iPhone Application Development
MKUserLocation • Special case of an MKAnnotation • Represents device’s location only @property BOOL updating (getter = isUpdating); @property CLLocation *location; @property NSString *title; @property NSString *subtitle; EEC492/693/793 - iPhone Application Development
MKReverseGeocoder • Given a location, what’s the human-readable address? - (void)initWithCoordinate:(CLLocationCoordinate2D)coordinate; @property id <MKReverseGeocoderDelegate> delegate; - (void)start; - (void)cancel; • Delegate callbacks: - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark; - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error; EEC492/693/793 - iPhone Application Development