【问题标题】:Get coordinates once with CLLocation manager in iOS在 iOS 中使用 CLLocation 管理器获取一次坐标
【发布时间】:2013-02-24 23:25:38
【问题描述】:

我正在使用此代码获取坐标:

_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];

然后我正在使用

- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations
{

_currentLocation = [locations lastObject];


//Doing some stuff

//I am using stopUpdatingLocation but this delegate is called a lot of times
[_locationManager stopUpdatingLocation];



 }

我实际上想获得一次坐标,因为我想避免多次执行 didUpdateLocation 中的代码。我该怎么做?

【问题讨论】:

    标签: ios geolocation cllocationmanager


    【解决方案1】:

    这是因为位置管理器多次触发其更新,逐步获得更准确的位置结果。

    阻止这种情况的一种方法是简单地使用布尔值

    BOOL _hasUpdatedUserLocation = NO;
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        if (!_hasUpdatedUserLocation) {
            _hasUpdatedUserLocation = YES;
            ... // other code
        }
    }
    

    或者,您可以杀死代表:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        [locationManager setDelegate:nil];
        ... // other code
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多