ios – 获取用户的经度/纬度当viewDidLoad

前端之家收集整理的这篇文章主要介绍了ios – 获取用户的经度/纬度当viewDidLoad前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的目的是在加载视图时获取用户的经度/纬度,并将值存储在两个变量中以供以后计算.我不需要跟踪用户的位置并更新它,我只需要一次他/她的坐标.我的代码如下所示:
- (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开始更新位置(您这样做)并实现管理员在检索位置时调用的委托方法,即可获取用户的位置 – 您无法立即从位置管理器获取位置.

如果您不想跟踪用户位置 – 只需在第一次获取位置后停止更新该委托方法中的位置(如果您以后需要,则将其存储).

原文链接:https://www.f2er.com/iOS/336550.html

猜你在找的iOS相关文章