我的目的是在加载视图时获取用户的经度/纬度,并将值存储在两个变量中以供以后计算.我不需要跟踪用户的位置并更新它,我只需要一次他/她的坐标.我的代码如下所示:
- (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开始更新位置(您这样做)并实现管理员在检索位置时调用的委托方法,即可获取用户的位置 – 您无法立即从位置管理器获取位置.
如果您不想跟踪用户位置 – 只需在第一次获取位置后停止更新该委托方法中的位置(如果您以后需要,则将其存储).