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

时间:2022-05-17 21:54:12

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

Can you please help me there?

你能帮帮我吗?

5 个解决方案

#1


7  

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.

为了获得用户的位置,即使您需要使locationManager开始更新位置(您这样做)并实现管理员在检索位置时调用的委托方法 - 您也无法立即从位置管理器获取位置。

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).

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

#2


7  

As soon as you call [locationManager startUpdatingLocation]; your viewDidLoad method is done. You need to implement

一旦调用[locationManager startUpdatingLocation];你的viewDidLoad方法已经完成。你需要实施

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

and

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

which will be called by your locationManager when it gets the location, or when it fails. In both of those methods, you can call [locationManager stopUpdatingLocation].

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

Inside the didUpdateToLocation method, you should move all your code from viewDidLoad, starting with CLLocation *location = [locationManager location]; That's where you'll get proper values.

在didUpdateToLocation方法中,您应该从viewDidLoad移动所有代码,从CLLocation * location = [locationManager location]开始;这就是你获得正确价值的地方。

#3


6  

This worked for me

这对我有用

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

#4


3  

Try this Code

试试这个代码

-(IBAction)Button:(id)sender

{

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];

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];

}

#5


1  

if you are getting Lat lang both (0.00 ,0.00)

如果你得到Lat lang两者(0.00,0.00)

this may be solution for your ,

这可能是你的解决方案,

step 1: NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription

步骤1:NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription

Add these two key in Info.plist

在Info.plist中添加这两个键

step 2: And than [locationManager startUpdatingLocation]; start updating

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

step 3: and you need to implement two mehthods

第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);



}

of CLLocationManagerDelegate in your ViewController interface.

ViewController接口中的CLLocationManagerDelegate。

**step 4:**

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


}

#1


7  

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.

为了获得用户的位置,即使您需要使locationManager开始更新位置(您这样做)并实现管理员在检索位置时调用的委托方法 - 您也无法立即从位置管理器获取位置。

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).

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

#2


7  

As soon as you call [locationManager startUpdatingLocation]; your viewDidLoad method is done. You need to implement

一旦调用[locationManager startUpdatingLocation];你的viewDidLoad方法已经完成。你需要实施

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

and

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

which will be called by your locationManager when it gets the location, or when it fails. In both of those methods, you can call [locationManager stopUpdatingLocation].

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

Inside the didUpdateToLocation method, you should move all your code from viewDidLoad, starting with CLLocation *location = [locationManager location]; That's where you'll get proper values.

在didUpdateToLocation方法中,您应该从viewDidLoad移动所有代码,从CLLocation * location = [locationManager location]开始;这就是你获得正确价值的地方。

#3


6  

This worked for me

这对我有用

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

#4


3  

Try this Code

试试这个代码

-(IBAction)Button:(id)sender

{

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];

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];

}

#5


1  

if you are getting Lat lang both (0.00 ,0.00)

如果你得到Lat lang两者(0.00,0.00)

this may be solution for your ,

这可能是你的解决方案,

step 1: NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription

步骤1:NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription

Add these two key in Info.plist

在Info.plist中添加这两个键

step 2: And than [locationManager startUpdatingLocation]; start updating

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

step 3: and you need to implement two mehthods

第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);



}

of CLLocationManagerDelegate in your ViewController interface.

ViewController接口中的CLLocationManagerDelegate。

**step 4:**

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


}