我的目的是在加载 View 时获取用户的经度/纬度,并将值存储在两个变量中以供以后计算。我不需要跟踪用户位置并更新它,我只需要获取他/她的坐标一次。我的代码如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// locationManager update as location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
float longitude=coordinate.longitude;
float latitude=coordinate.latitude;
NSLog(@"dLongitude : %f",longitude);
NSLog(@"dLatitude : %f", latitude);
}
在控制台中我不断得到这个:
dLongitude : 0.000000
dLatitude : 0.000000
你能帮我吗?
最佳答案
要获取用户的位置,即使您需要让 locationManager
开始更新位置(您这样做了)并实现在检索位置时管理器调用的委托(delegate)方法 - 您无法从位置获取位置经理立即。
如果您不想跟踪用户位置 - 只需在第一次获取位置后停止更新该委托(delegate)方法中的位置(并在将来需要时存储它)。
关于ios - viewDidLoad时获取用户的经度/纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6016665/