【问题标题】:Getting Longitude/Latitude of the User When the viewDidLoadviewDidLoad时获取用户的经度/纬度
【发布时间】:2011-08-26 09:03:19
【问题描述】:

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

- (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

你能帮帮我吗?

【问题讨论】:

  • 你是在设备还是模拟器上测试?
  • 您可以通过模拟器模拟您的位置,这并不重要。

标签: ios core-location


【解决方案1】:

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

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

【讨论】:

  • 嗨,在locationManager 中,我将 lon/lat 值分配给变量后如何停止更新位置,我这样做了 '[locationManager stopUpdatingLocation]',够了吗?
  • @Malek,是的,这应该足以停止跟踪位置
  • 好的,所以我不必实现stopUpdatingLocation委托方法?
  • 谢谢弗拉基米尔,顺便说一句,我试图让用户long/lat 并且在控制台中他们仍然0.000000,我认为我的代码一点都没有错,但是我开始确定这都是关于模拟器的东西,你不觉得吗?
【解决方案2】:

只要您调用[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]; 开头,这样您将获得正确的值。

【讨论】:

  • 这正是我所做的,但是,我得到的经度和纬度值是这个值:0.000000 这就是为什么我仍然对这是由于什么感到困惑,是吗由于模拟器,如果是这样,我不必担心我在设备上测试我的应用程序:)
【解决方案3】:

这对我有用

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

【讨论】:

  • 最好使用委托回调并省略[locationManager stopUpdatingLocation]的使用;就在开始之后。停止可以在卸载方法中完成。
【解决方案4】:

试试这个代码

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

}

【讨论】:

    【解决方案5】:

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

    这可能是您的解决方案,

    第 1 步: NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

    Info.plist中添加这两个key

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

    CLLocationManagerDelegate 在您的 ViewController 界面中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多