Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Using Maps in your iOS apps MapKit > Time for action — using MapKit in your ...

Time for action — using MapKit in your app

Time for action — using MapKit in your app We will use the Hello Location application we saw in Chapter 3, the one that uses location updates in our app, so that we can change the map display as the location changes. The following diagram should give you an idea of our app behavior: We modify the Hello_LocationViewController.xib file to make our app look more professional as follows: Add a UINavigationBar to your application's NIB file. Create another view by dragging it from the Toolbox onto the NIB file. Your app should now look like the following screenshot; do not get confused with the UIView already present. What we will be learning is superimposing another UIView (for this example, it will contain the map) onto the Main UIView of the application. Now we map the Navigation Bar's Show Map button to the locationDetect action. In your Hello_LocationViewController.h file, import the MapKit Framework and define the MKMapView object, as well as the Map View you defined in step 1. Your code should now look like following snippet: #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface Hello_LocationViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locMgr; CLLocation *userLocation; NSString *message; MKMapView *map; UIView *mapView; } @property (retain,nonatomic) MKMapView *map; @property (strong, nonatomic) IBOutlet UIView *mapView; - (IBAction)locationDetect:(id)sender; @end In your Hello_LocationViewController.m file, synthesize the map and mapview objects: @synthesize map; @synthesize mapView; Furthermore, create an MKCoordinateRegion object that will hold the portion of the map to display as follows: MKCoordinateRegion region; In the viewDidLoad method, we create the map object and bind it to the new View we created to hold the map; the map type is the default Google Maps standard view, which is as follows: map = [[MKMapView alloc]initWithFrame:self.mapView.bounds]; map.mapType = MKMapTypeStandard; We define the region to show on the map from the user's initial location (San Francisco, if the location is not found; the actual location otherwise). The zoom level is controlled by the span variable of the region. Next, we enable the map zooming. The complete viewDidLoad method is as shown in the following code snippet: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. locMgr = [[CLLocationManager alloc]init]; locMgr.desiredAccuracy= kCLLocationAccuracyBest; locMgr.distanceFilter=1000.0f; locMgr.delegate=self; userLocation = [[CLLocation alloc] initWithLatitude:37.33 longitude:-122.03]; message = [[NSString alloc]initWithString:@""]; if([CLLocationManager locationServicesEnabled]==NO) { message=@"Location cannot be initialized. Please check settings"; } [locMgr startUpdatingLocation]; map = [[MKMapView alloc] initWithFrame:self.mapView.bounds]; map.mapType = MKMapTypeStandard; region.center = userLocation.coordinate; region.span.latitudeDelta = 0.1; region.span.longitudeDelta = 0.1; map.zoomEnabled = TRUE; [map setRegion:region animated:TRUE]; } When the location changes, we use the new location values and pass it to the region variable and update the map in the Location Manager's didUpdateToLocation method as follows: (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { region.center = newLocation.coordinate; [map setRegion:region animated:TRUE]; } On the Show Map button click, we defined the locationDetect method to be called. Based on the user's authorization, we define the region of the map accordingly, and then, finally updated the map. The map is not rendered on the View unless we add it as a subView to the mapView we created in step 1, 2, and 3, as follows: [self.mapView addSubview:map]; The following is the updated locationDetect method: - (IBAction)locationDetect:(id)sender { if([CLLocationManager locationServicesEnabled]==TRUE) { if([CLLocationManager authorizationStatus]== kCLAuthorizationStatusNotDetermined) { message = @"User hasn't made a choice yet. Defaulting to San Francisco"; region.center = userLocation.coordinate; } else if([CLLocationManager authorizationStatus]== kCLAuthorizationStatusDenied) { message = @"User has denied use of location services for your application or all applications.Defaulting to San Francisco"; region.center = userLocation.coordinate; } else if([CLLocationManager authorizationStatus]== kCLAuthorizationStatusAuthorized) { message = @"User has authorized your application for location services."; region.center = locMgr.location.coordinate; } else if([CLLocationManager authorizationStatus]== kCLAuthorizationStatusRestricted) { message = @"Not authorized to user location services.Defaulting to San Francisco"; region.center = userLocation.coordinate; } [map setRegion:region animated:TRUE]; [self.mapView addSubview:map]; } else if([CLLocationManager locationServicesEnabled]==FALSE) { message = @"Location cannot be initialized. Please check settings"; } } Run the application on the iOS simulator by using Location Simulation. You should see the following result when you click on the Show Map button on the navigation bar: What just happened? We created a simple app that detected a user's location updates and displayed a map. We combined Core Location and MapKit functionality to do so. Try changing the location values via the Location Simulation feature in iOS 5 in the Product | Debug | Simulate Location menu option and see the map change to the simulated location. Note the map should be rendered once to observe this change. Do not forget to include the MapKit Library reference in your project. You can find the code for this example on the book's website, project titled Hello Location - With Maps.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint