我想获取 iphone 上的当前位置并将坐标用于某些函数调用。
现在我有一个问题,当我想访问坐标时,它们是空的。但是在委托函数上,它们以某种方式被检索,但没有被存储......
NSLog 中doLocation
不显示任何值,但在locationmanager
NSLog 中显示。或者这可能是一个 NSLog 问题?
#pragma mark - Location handling
-(CLLocationCoordinate2D)doLocation {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = 50;
[self.locationManager startUpdatingLocation];
CLLocation *location = [self.locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSLog(@"debug: coordinates: %@", coordinate);
return coordinate;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (!oldLocation ||
(oldLocation.coordinate.latitude != newLocation.coordinate.latitude &&
oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {
currentLat = newLocation.coordinate.latitude;
currentLng = newLocation.coordinate.longitude;
} else { // oldLocation
currentLat = oldLocation.coordinate.latitude;
currentLng = oldLocation.coordinate.longitude;
}
self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng];
NSLog(@"debug: self.currentLoc: %@", self.currentLoc);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"%@", error);
}
调试出来的是:
2013-07-01 15:59:29.382 foobar[3793:c07] 调试:坐标:(空)-doLocation 1:当前位置:
2013-07-01 15:59:35.399 foobar[3793:c07] 调试:self.currentLoc:<+51.50998000,-0.13370000> +/- 0.00m(速度 -1.00 mps / 课程 -1.00)@ 7/1/13 , 下午 3:59:35 中欧夏令时间