Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 18: Where Am I? Finding Your Way with Core Location

643

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

NSString *errorType = (error.code == kCLErrorDenied) ? @"Access Denied" : @"Unknown Error";

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting Location" message:errorType

delegate:nil

cancelButtonTitle:@"Okay"

otherButtonTitles:nil];

[alert show];

}

@end

In the viewDidLoad method, we allocate and initialize a CLLocationManager instance, assign our controller class as the delegate, set the desired accuracy to the best available, and then tell our location manager instance to start giving us location updates.

- (void)viewDidLoad {

self.locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation];

}

Updating Location Manager

Since this class designated itself as the location manager’s delegate, we know that location updates will come into this class if we implement the delegate method locationmanager:didUpdateToLocation:fromLocation:. Now, let’s look at our implementation of that method.

The first thing we do in the delegate method is check whether startingPoint is nil. If it is, then this update is the first one from the location manager, and we assign the current location to our startingPoint property.

if (startingPoint == nil) self.startingPoint = newLocation;

After that, we update the first six labels with values from the CLLocation object passed in the newLocation argument.

NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0", newLocation.coordinate.latitude];

latitudeLabel.text = latitudeString;

NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0", newLocation.coordinate.longitude];

longitudeLabel.text = longitudeString;

NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm", newLocation.horizontalAccuracy];

horizontalAccuracyLabel.text = horizontalAccuracyString;

NSString *altitudeString = [NSString stringWithFormat:@"%gm",

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]