CoreLocation


#1

I am going back through this book to clarify concepts that I may not have completely understood the first time. When I looked at ‘whereami’ I became confused about why we are using CoreLocation services at all. MapKit gives us the only thing we are really interested in for the application which is current location. In the example code after a user enters annotation text we call findlocation, which turns on location services, then location services returns our current location, then we annotate it. Wouldn’t it be a lot more efficient to store the coordinate from mapView: didUpdateUserLocation in an instance variable and then annotate that point once the textFieldShouldReturn method is executed? My code below:

//
// WhereamiViewController.h
// Whereami
//
// Created on 2/15/13.
// Copyright © 2013 marc koffskey. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController <MKMapViewDelegate, UITextFieldDelegate>
{
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
CLLocationCoordinate2D saveCoord;
}

@end

//
// WhereamiViewController.m
// Whereami
//
// Created on 2/15/13.
// Copyright © 2013 marc koffskey. All rights reserved.
//

#import “WhereamiViewController.h”
#import “BNRMapPoint.h”

@implementation WhereamiViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
    }

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [worldView setShowsUserLocation:YES];
    }

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • (void)mapView: (MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    saveCoord = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(saveCoord, 250, 250);
    [worldView setRegion:region animated:YES];
    }

  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {

    BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordinate:saveCoord title:[locationTitleField text]];
    [worldView addAnnotation:mp];

    [locationTitleField setText:@""];

    [textField resignFirstResponder];
    return YES;
    }


#2

I was thinking the same thing… The use of CoreLocation seems redundant here, as the only information we need is already supplied by worldView. I suspect that the choice to do it this way was just to expand upon what was done in the previous chapter, although I must say if that was the reason I don’t really agree with it, it just creates confusion. To test to see if CL was really needed, I replaced (in textFieldShouldReturn:)

[self findLocation];

with

[self foundLocation:[[worldView userLocation] location]];

An the app appears to be functioning exactly the same, yet now has no dependency on CoreLocation.

It seems you have discovered this peculiarity many months ago. If you have discovered any further insight on this, please share.