- (void) setupLocationManager { self.locationManager = [[[CLLocationManager alloc] init] autorelease]; if (locationManager.locationServicesEnabled == NO) { UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [servicesDisabledAlert show]; [servicesDisabledAlert release]; } locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; self.stateString = NSLocalizedString(@"Updating", @"Updating"); } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // test that the horizontal accuracy does not indicate an invalid measurement if (newLocation.horizontalAccuracy < 0) return; // test the age of the location measurement to determine if the measurement is cached // in most cases you will not want to rely on cached measurements NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 5.0) return; if ([newLocation speed] < 0.0f) { [g_Config SetSpeed:0]; [g_Config SetCourseValue]; } else { float fSpeed = [newLocation speed]; [g_Config SetRealSpeed:fSpeed]; [g_Config SetCourseValue:newLocation.course]; } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"didFailWithError"); [g_Cofig SetSpeed:0]; [g_Config SetCourseValue:0]; // The location "unknown" error simply means the manager is currently unable to get the location. // We can ignore this error for the scenario of getting a single location fix, because we already have a // timeout that will stop the location manager to save power. if ([error code] != kCLErrorLocationUnknown) { [self stopUpdatingLocation:NSLocalizedString(@"Error", @"Error")]; } } - (void)stopUpdatingLocation:(NSString *)state { self.stateString = state; NSLog(@"%@", state); [locationManager stopUpdatingLocation]; locationManager.delegate = nil; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)