Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 限制反向地理定位-kCLErrorDomain代码=2_Ios_Objective C_Geolocation_Gps_Reverse Geocoding - Fatal编程技术网

Ios 限制反向地理定位-kCLErrorDomain代码=2

Ios 限制反向地理定位-kCLErrorDomain代码=2,ios,objective-c,geolocation,gps,reverse-geocoding,Ios,Objective C,Geolocation,Gps,Reverse Geocoding,我目前正在尝试编写一个iOS应用程序,记录用户访问过的状态 我面临的问题是我的(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations方法被反复调用。我不一定认为这是一个问题,除了在该方法中,我正在调用另一个方法,该方法对CLLocation对象进行反向地理编码并给出状态名称 我得到的错误是: Geocode failed with error: Error Domain=kC

我目前正在尝试编写一个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.)"
我知道我在一定的时间期限内达到了反向编码的极限,我只是不知道如何限制它

这是我的密码:

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属性,如下所述:

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


希望这有帮助

多谢各位!没想到我能做到!我有一个问题。当我运行StartUpdatingLocation时,它最初会运行一次以获取我的当前位置吗?是的,只要它是CLLocationManager的新实例,它就会运行。