1 / 28

iPhone Application Development: Outline Maps and Locations

Learn about Core Location Framework, obtaining location information, CLLocation Manager, CLLocation, CLLocation Manager Delegate, CLLocation Manager Delegate protocol, and using MapKit for map display.

vbechtel
Download Presentation

iPhone Application Development: Outline Maps and Locations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEC-492/693/793iPhone Application Development Lecture 17 Wenbing Zhao & Nigamanth Sridhar EEC492/693/793 - iPhone Application Development

  2. 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

  3. Core Location Framework • Classes • CLLocationManager • CLLocation • CLHeading • Protocol • CLLocationManagerDelegate • No UI EEC492/693/793 - iPhone Application Development

  4. How to Obtain Location Information • Three tiered approach • GPS • Wifi • Cell network EEC492/693/793 - iPhone Application Development

  5. 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

  6. 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

  7. 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

  8. Getting a Location:Starting the location service CLLocationManager* locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; [locManager startUpdatingLocation]; EEC492/693/793 - iPhone Application Development

  9. 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

  10. 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

  11. 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

  12. 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

  13. Stopping the Service CLLocationManager* locManager = [[CLLocationManager alloc] init]; [locManager startUpdatingLocation]; ... [locManager stopUpdatingLocation]; EEC492/693/793 - iPhone Application Development

  14. 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

  15. Responding to ErrorsLocation may be unavailable • Results in a kCLErrorLocationUnknown error • Likely just temporary • Scan continues in background EEC492/693/793 - iPhone Application Development

  16. 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

  17. MKMapView • Handles display of map • “Map” & “Satellite” types • Panning and Zooming • Annotations • Display User Location EEC492/693/793 - iPhone Application Development

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

More Related