这可能是我想要的简单事情,但我已经设置了位置服务(为了清晰起见缩短了):
- (void)viewDidLoad
{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@",newLocation.coordinate.latitude);
NSLog(@"%@",newLocation.coordinate.longitude);
}
Run Code Online (Sandbox Code Playgroud)
工作正常,并给我一个位置数据流到日志.
但我想要的是能够立即在ViewDidLoad中获取当前位置,因为我只需要它一次,而不是一次不断更新 - 它只是为了找到"最近"的便利设施,所以我可以向用户报告.我试过添加:
self.locationLat = [self.locationManager location].coordinate.latitude;
self.locationLng = [self.locationManager location].coordinate.longitude;
Run Code Online (Sandbox Code Playgroud)
在startUpdatingLocation之后立即到ViewDidLoad,但它们总是以null形式出现.还有什么我需要调用才能在运行后获取数据吗?
谢谢
Rom*_*nko 10
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
/*report to user*/
[self.locationManager stopUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)
因此,您将获得一次位置,然后停止更新它.