下面是我的CLLocationManagerDelegate的代码。我使用:
调用委托方法locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
委托方法:
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//get current location of phone:
if (newLocation != nil) {
_m.longitude = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude];
_m.latitude = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude];
_m.timestamp = newLocation.timestamp;
NSLog(@"didUpdateToLocation: %@", newLocation);
}
CLLocation *currentLocation = newLocation;
}
当我调用didUpdateToLocation方法时,我已经通过调试验证了newLocation不是nil。 _m是在我的标题中声明的对象,也不是nil。对于locationManager也是如此。但是,当我尝试设置_m.latitude等和NSLog时,它们都显示为(null)。 didUpdateToLocation NSLog产生如下行:
didUpdateToLocation:< + 47.64312748,-122.12163262> +/- 65.00米(速度-1.00 mps /航向-1.00)@ 7/15 / 14,19:26:07太平洋夏令时
我不确定我哪里出错了。
答案 0 :(得分:0)
您的代码看起来是正确的。所以你可能在日志中得到null,因为_m == nil
。