Graphics Programs Reference
In-Depth Information
Being a MapView Delegate
When
Whereami
launches, we want it to find the current location and display it on a map.
In the last chapter, you worked directly with Core Location to find the user's location. Now
this won't be necessary because an instance of
MKMapView
knows how to use Core Loca-
tion to find the user's location. All you have to do is set the
showsUserLocation
prop-
erty of an
MKMapView
to
YES
, and it will find and show the user's location on the map.
After the interface loads, the
WhereamiViewController
is sent the message
viewDidLoad
. This is when you will tell the
MKMapView
to update its location. (We'll
iViewController.m
.
- (void)viewDidLoad
{
[worldView setShowsUserLocation:YES];
}
Now that we have the
MKMapView
to determine the location, we don't need the
loca-
tionManager
to do it. Remove the line of code that tells the
locationManager
to
start updating in
WhereamiViewController.m
. Leave the rest of the setup code for
the location manager in place.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
return self;
}
Build and run the application. Once the application launches, the map will display a blue
annotation dot on your current location. If you are using the simulator, choose a location to
simulate by clicking the
icon in the debugger bar and selecting an option.)
Unfortunately, you are looking at a map of the entire world, so the blue dot that identifies
your location is the size of Brazil. The application clearly needs to zoom in on the current
location to be useful. Let's figure out when and how we can make it do this.