1 / 24

Location and Map Services

Location and Map Services. CSE 391 Fall 2011 Tony Scarlatos. Where Am I?. The goal of this demo is to build a simple app that will return the user’s location in latitude and longitude values.

keon
Download Presentation

Location and Map Services

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. Location and Map Services CSE 391 Fall 2011 Tony Scarlatos

  2. Where Am I? • The goal of this demo is to build a simple app that will return the user’s location in latitude and longitude values. • After creating a basic View-based Application in Xcode, the first step is to set up the interface in IB. In this simple example all the elements can be labels. The latLabel and longLabel are defined as IBOutlets in the viewController.h file. These outlets need to be connected to the respective labels in Interface Builder.

  3. The Core Location Framework • You will have to add the Core Location framework to your Xcode project. Select the target, choose the Build Phases tab, and select Link Binary with Libraries. Choose the + icon to add the Core Location framework. • The most important class in the Core Location framework is the CLLocationManager, which uses the CLLocationManagerDelegate protocol, and this must be defined in the viewController.h file. • The other thing that has to be added is an instance variable for the loccation manager. • The code for the header file is on the next slide.

  4. The viewController.h file #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> //don't forget! @interface WhereAmIViewController : UIViewController <CLLocationManagerDelegate> { //tells the app we are using the CLLocationManagerDelegate protocol CLLocationManager *locationManager; //instance variable for the locationManager IBOutletUILabel *latLabel; IBOutletUILabel *longLabel; } @end

  5. Setting up the Location Manager • In the viewController.m file we will use the viewDidLoad method to set up the location manager and get it to start updating. • The delegate for the CLLocationManager is set to the viewController (self). • The distanceFilter property establishes how far the user must move before it sends an update, and kCLDistanceFilterNone means that the updates will be continuous (with values in kilometers). • The desiredAccuracy property determines the accuracy of the phone’s location data to be included in an update, and here it is set fairly broad – kCLLocationAccuracyHundredMeters (100m). • The code for the viewDidLoad method follows on the next slide.

  6. The viewDidLoad method • (void)viewDidLoad { [superviewDidLoad]; locationManager = [[CLLocationManageralloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; // whenever the location changes locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [locationManagerstartUpdatingLocation]; }

  7. The delegate method • The delegate method is called when there is a change in location. Old and new location information comes in as CLLocation objects. The CLLocation objects have coordinates, accuracy, altitude, and timestamp information (we are only using the coordinates in this example). • The rest of the code converts the values into degrees, minutes, and seconds for both latitude and longitude. The calculations are converted to a formatted NSString and assigned to their respective labels in the app. • The code follows on the next slide.

  8. CLLocationManager method • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { int degrees = newLocation.coordinate.latitude; double decimal = fabs(newLocation.coordinate.latitude - degrees); int minutes = decimal * 60; double seconds = decimal * 3600 - minutes * 60; NSString *lat = [NSStringstringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; latLabel.text = lat; degrees = newLocation.coordinate.longitude; decimal = fabs(newLocation.coordinate.longitude - degrees); minutes = decimal * 60; seconds = decimal * 3600 - minutes * 60; NSString *longt = [NSStringstringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; longLabel.text = longt; }

  9. Success!

  10. Displaying an annotated map view • Start with a View-based Application template. • Import the MapKit framework. • In the viewController.h file import the mapKit framework and create an instance variable (mapView) for MKMapView of the IBOutlet type. • Open up the viewController.xib file in IB, and drag a map view on to the view pane. • Control-click on File’s Owner to connect the mapView outlet to the map view object. • From the document window control-click the map view to connect to the delegate connection of File’s Owner.

  11. The viewController.h file • In the view controller header file you’ll need to add an address annotation variable and a method for showing the address. • You’ll also need to add an internal interface for getting the coordinates and adding titles and subtitles – an address annotation interface. • The code follows on the next slide.

  12. The viewController.h code #import <UIKit/UIKit.h> #import <MapKit/MapKit.h>// don't forget! @interface AddressAnnotation : NSObject <MKAnnotation> { CLLocationCoordinate2D coordinate; NSString *title; NSString *subTitle; } @end @interface MapTutorialViewController : UIViewController { IBOutletMKMapView *mapView; AddressAnnotation *addAnnotation; } - (IBAction) showAddress; @end

  13. The AddressAnnotation implementation You can now add the AddressAnnotation implementation to the viewController.m file. In our example it was added above the @implementation MapTutorialViewContoller statement. @implementation AddressAnnotation //implemetation from .h file @synthesize coordinate; • (NSString *)subtitle { return@"Sub Title"; } • (NSString *)title { return@"Title"; } -(id)initWithCoordinate:(CLLocationCoordinate2D) c { coordinate=c; NSLog(@"%f,%f",c.latitude,c.longitude);returnself; } @end

  14. The MapTutorialViewController implementation You can now add the the showAddress method in the MapTutorialViewController implementation code. showAddress is called from the viewDidLoad method. • (void)viewDidLoad { [superviewDidLoad]; [selfshowAddress]; }

  15. The showAddress method • (IBAction) showAddress { MKCoordinateRegion region; MKCoordinateSpan span; span.latitudeDelta=0.2; span.longitudeDelta=0.2; CLLocationCoordinate2D location = mapView.userLocation.coordinate; location.latitude = 40.911298;// the coordinates for the Long Island Museum location.longitude = -73.142098; region.span=span; region.center=location; if(addAnnotation != nil) { [mapViewremoveAnnotation:addAnnotation]; [addAnnotationrelease];addAnnotation = nil; } addAnnotation = [[AddressAnnotationalloc] initWithCoordinate:location]; [mapViewaddAnnotation:addAnnotation]; [mapViewsetRegion:region animated:TRUE]; [mapViewregionThatFits:region]; //[mapView selectAnnotation:mLodgeAnnotation animated:YES]; }

  16. The mapView:showForAnnotation method Right after the showAddress method you can add the mapView:showForAnnotation method. • (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation { MKPinAnnotationView *annView=[[MKPinAnnotationViewalloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; annView.pinColor = MKPinAnnotationColorGreen; annView.animatesDrop=TRUE; annView.canShowCallout = YES; annView.calloutOffset = CGPointMake(-5, 5); return annView; }

  17. Success!

  18. Adding a Title and Subtitle Modify the AddessAnnotation implementation to allow Titles and Subtitles. • (NSString *)subtitle { returnsubTitle; } • (void)setSubTitle:(NSString *)subtitleIn { subTitle = subtitleIn; } • (NSString *)title { returntitle; } • (void)setTitle:(NSString *)titleIn { title = titleIn; }

  19. Modify the showAddress method addAnnotation = [[AddressAnnotationalloc] initWithCoordinate:location]; [addAnnotationsetTitle:@"Long Island Museum"]; [addAnnotationsetSubTitle:@"History, Art, and Carriages"]; [mapViewaddAnnotation:addAnnotation];

  20. Success!

  21. Add additional locations Include additional locations in the viewController.h file. @interface MapTutorialViewController : UIViewController { IBOutletMKMapView*mapView; AddressAnnotation *addAnnotation; AddressAnnotation *addAnnotation2; }

  22. Modify the showAddress method CLLocationCoordinate2D location2 = mapView.userLocation.coordinate; location2.latitude = 40.860482; //coordinates of the Mills Pond House location2.longitude = -73.155731; if(addAnnotation2 != nil) { [mapViewremoveAnnotation:addAnnotation2]; [addAnnotation2release]; addAnnotation2 = nil; } addAnnotation2 = [[AddressAnnotationalloc] initWithCoordinate:location2]; [addAnnotation2setTitle:@"Mills Pond House"]; [addAnnotation2setSubTitle:@"Public Gallery Space"]; [mapViewaddAnnotation:addAnnotation2];

  23. Success!

  24. Wrapping up… • You can get latitude and longitude data for any address by using the free service at http://geocoder.us/ • By also including the Core Location framework you can add the user’s location data to your map app. Just check off the “Shows User Location” option in Interface Builder for the map view. • Happy mapping!

More Related