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

Joh*_*rad 3 gps objective-c geolocation reverse-geocoding ios

我目前正在尝试编写一个记录用户访问状态的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.)"
Run Code Online (Sandbox Code Playgroud)

我知道我在一定的时间限制内达到了反向地理编码的限制,我只是不确定如何限制它.

这是我的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

此代码完美地运行,它获取GPS坐标中的位置,将其转​​换为州名称,并将该州名称写入文件.

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

csu*_*man 5

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

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

希望这可以帮助!