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

时间:2011-05-16 11:25:37

标签: ios core-location

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

- (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
你能帮助我吗?

5 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:7)

只要您致电[locationManager startUpdatingLocation];,您的viewDidLoad方法就完成了。你需要实现

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

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

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

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

答案 2 :(得分:6)

这对我有用

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

答案 3 :(得分:3)

试用此代码

-(IBAction)Button:(id)sender

{

locationManager = [[CLLocationManager alloc] init]; 

locationManager.delegate = self;

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

locationManager.distanceFilter = kCLDistanceFilterNone;

[locationManager startUpdatingLocation];

CLLocation *location = [locationManager 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];

}

答案 4 :(得分:1)

如果您同时获得Lat lang(0.00,0.00)

这可能是您的解决方案,

第1步: NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

Info.plist

中添加这两个键

第2步: 并且[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];


}