获取用户的经度/纬度时viewDidLoad [英] Getting Longitude/Latitude of the User When the viewDidLoad

查看:169
本文介绍了获取用户的经度/纬度时viewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是在加载视图时获取用户的经度/纬度,并将值存储在两个变量中以便以后计算。我不需要跟踪用户位置并更新它,我只需要将他/她的坐标设置为。我的代码如下所示:

My purpose is to get the longitude/latitude of the user when the view is loaded and to store the values in two variables for later calculations. I don't need to track the user location and to update it, I simply need to get his/her coordinates once. My code looks like this:

- (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); 
}

在控制台中我一直这样:

In the console i keep getting this:

dLongitude : 0.000000
dLatitude : 0.000000

你能帮助我吗?

推荐答案

一旦你需要制作<$ c来获取用户的位置$ c> locationManager 开始更新位置(您这样做)并实现管理员在检索位置时调用的委托方法 - 您无法立即从位置管理器获取位置。

To get user's location even once you need to make locationManager to start updating locations (you did that) and implement delegate method that manager calls when location is retrieved - you can't get location from location manager immediately.

如果您不想跟踪用户位置 - 只需在第一次获取位置后停止更新该委托方法中的位置(并在需要时存储它)将来)。

If you don't want to track user location - just stop updating locations in that delegate method after location was fetched for the 1st time (and store it if you need it in future).

这篇关于获取用户的经度/纬度时viewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆