我目前正在尝试编写一个 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/