ios - 限制反向地理定位 - kCLErrorDomain Code=2

标签 ios objective-c geolocation gps reverse-geocoding

我目前正在尝试编写一个 iOS 应用程序来记录用户访问过的状态。

我面临的问题是我的 (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 方法被一遍又一遍地调用。除了那个方法,我不一定认为这是一个问题,我正在调用另一个方法来反向地理编码 CLLocation 对象并给出状态名称。

我得到的错误是:

Geocode failed with error: Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)"

我知道我在某个时间限制内达到了 reverseGeoCoding 的限制,我只是不确定如何限制它。

这是我的代码:

CLLocation *currentLocation;

- (void) getLocation
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
     {

         if (error){
             NSLog(@"Geocode failed with error: %@", error);
             return;

         }

         if(placemarks && placemarks.count > 0)
         {
             CLPlacemark *placemark= [placemarks objectAtIndex:0];
             currentState = [NSString stringWithFormat:@"%@",[placemark administrativeArea]];
         }

         //Checks to see if the state exists in the textFile, if not it writes it to the file
         if(![newState checkIfStateExists:currentState])
         {
             [newState writeToFile:currentState];
         }

     }];
}

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //just a thought, didn't work
    //if(![[locations lastObject] isEqual:currentLocation])

        currentLocation = [locations lastObject];
        [self getLocation];

}

-(void) initializeGPS
{
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}


- (void) viewDidLoad
{
    [self initializeGPS];
    [super viewDidLoad];

}

这段代码非常有效,因为它获取 GPS 坐标中的位置,将其转​​换为州名称,并将该州名称写入文件。

它只是被调用了太多次,我不确定如何限制 (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 被调用的次数.

最佳答案

使用 CLLocationManager 的 distanceFilter 属性,如下所述:https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

distanceFilter 属性允许您设置设备在更新位置之前必须移动的距离(以米为单位)。

希望这对您有所帮助!

关于ios - 限制反向地理定位 - kCLErrorDomain Code=2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310883/

相关文章:

objective-c - 如何记录应用程序 session 持续时间?

mysql - 查询 MySQL 以获取给定英里半径内的纬度和经度坐标

php - IP 地址转换为距特定邮政编码的距离

ios - URLSession didCompleteWithError 无错误

ios - 与导航 Controller 一起使用时不显示 UITabBarItem 的标题

ios - 更新到下一个 Xcode 获取编译器警告自动属性合成将不会合成属性

ios - insertRowsAtIndexPaths 的断言失败

iOS IBAction 方法永远不会被调用

ios - 加载图像需要时间

javascript - Meteor.js 响应式 html5 地理定位 position.coords