我正在使用CLLocationManager来确定我的当前位置:
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.currentLocation = newLocation;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (newLocation.horizontalAccuracy < 0) return;
if (self.currentLocation == nil || self.currentLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
self.currentLocation = newLocation;
}
[locationManager stopUpdatingLocation];
}
这段代码可以在iOS 5.1模拟器上正常运行,但在iOS 4.3模拟器上无法正常工作。我认为在iOS 5.1之前的真实设备上也可能会有问题。为什么它不能正常工作呢?
CLLocationManager
之前,请确保检查[CLLocationManager locationServicesEnabled]
,因为即使在模拟器中,您的位置服务也可能被关闭。 - Joe