这可能是我想要的简单事情,但我已经设置了位置服务(为了清晰起见缩短了):
- (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);
}
工作正常,并给我一个位置数据流到日志。
但我想要的是能够立即在ViewDidLoad中获取当前位置,因为我只需要它一次,而不是一次不断更新 - 它只是为了找到“最近”的便利设施,所以我可以向用户报告。我试过添加:
self.locationLat = [self.locationManager location].coordinate.latitude;
self.locationLng = [self.locationManager location].coordinate.longitude;
在startUpdatingLocation之后立即由于
答案 0 :(得分:10)
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
/*report to user*/
[self.locationManager stopUpdatingLocation];
}
因此,您将获得一次位置,然后停止更新。
答案 1 :(得分:3)
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingLocation];
NSLog(@"%@",newLocation.coordinate.latitude);
NSLog(@"%@",newLocation.coordinate.longitude);
}
答案 2 :(得分:2)
您只能获取
中的值- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
没有其他功能可用于获取位置值..因此,您获得的最快值是首次调用此函数时...