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

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

- (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开始更新位置(您这样做),并实现pipe理器调用位置检索时调用的委托方法,您无法立即从位置pipe理器获取位置。

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

只要你调用[locationManager startUpdatingLocation]; 你的viewDidLoad方法完成了。 你需要执行

 -(void) locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *) newLocation fromLocation: (CLLocation *) oldLocation 

 - (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 

这将由locationManager在获取位置或失败时调用。 在这两种方法中,都可以调用[locationManager stopUpdatingLocation]

在didUpdateToLocation方法中,你应该从viewLidLoad移动所有的代码,从CLLocation *location = [locationManager location]; 这就是你将得到适当价值的地方。

这对我有效

 - (void)viewDidLoad { [super viewDidLoad]; // locationManager update as location locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = kCLDistanceFilterNone; [locationManager startUpdatingLocation]; [locationManager stopUpdatingLocation]; CLLocation *location = [locationManager location]; // Configure the new event with information from the location float longitude=location.coordinate.longitude; float latitude=location.coordinate.latitude; NSLog(@"dLongitude : %f", longitude); NSLog(@"dLatitude : %f", latitude); } 

试试这个代码

 -(IBAction)Button:(id)sender { locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = kCLDistanceFilterNone; [locationManager startUpdatingLocation]; CLLocation *location = [locationManager location]; 

//使用来自位置的信息configuration新事件

 CLLocationCoordinate2D coordinate = [location coordinate]; NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; NSLog(@"dLatitude : %@", latitude); NSLog(@"dLongitude : %@",longitude); UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 40, 250, 50)]; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 200, 50)]; UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 200, 100)]; myLabel.textColor = [UIColor blackColor]; myLabel1.textColor = [UIColor blackColor]; label.backgroundColor = [UIColor clearColor]; myLabel.backgroundColor = [UIColor clearColor]; myLabel1.backgroundColor = [UIColor clearColor]; [myLabel setText:latitude]; [myLabel1 setText:longitude]; label.text = @"Current Latitude And Longitude"; [self.view addSubview:label]; [self.view addSubview:myLabel]; [self.view addSubview:myLabel1]; } 

如果你得到经纬度都(0.00,0.00)

这可能是你的解决scheme,

第1步: NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

Info.plist添加这两个键

第二步:[locationManager startUpdatingLocation]; 开始更新

第3步:你需要实施两个方法

 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation * currentLocation = [locations lastObject]; NSLog(@"Updated Lat %f",currentLocation.coordinate.longitude); NSLog(@"Updated Lang %f",currentLocation.coordinate.latitude); } 

您的ViewController界面中的CLLocationManagerDelegate

 **step 4:** -(void)viewDidLoad { [super viewDidLoad]; geocoder = [[CLGeocoder alloc] init]; manager.delegate = self; manager.desiredAccuracy = kCLLocationAccuracyBest; [manager requestWhenInUseAuthorization]; [manager requestAlwaysAuthorization]; [manager startUpdatingLocation]; }