3

我目前正在尝试编写一个记录用户访问过的状态的 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被调用的次数。

4

1 回答 1

5

使用此处概述的 CLLocationManager 的 distanceFilter 属性:https ://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

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

希望这可以帮助!

于 2013-05-01T00:12:12.167 回答