0

我想获得一个位置,然后停止从CLLocationManager.

我这样做:

-(id)initWithDelegate:(id <GPSLocationDelegate>)aDelegate{
self = [super init];

if(self != nil) {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    delegate = aDelegate;

}
return self;
}

-(void)startUpdating{
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [locationManager stopUpdatingLocation];
    [delegate locationUpdate:newLocation];
}

问题是即使我这样做[locationManager stopUpdatingLocation];

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:

我仍然收到通知,知道为什么会这样吗?

4

2 回答 2

2

也许试试我的解决方案。我正在构建两个函数来处理 LocationManger Obj。第一个函数是 startUpdates 用于处理开始更新位置。代码如下所示:

- (void)startUpdate
{
    if ([self locationManager])
    {
        [[self locationManager] stopUpdatingLocation];
    }
    else
    {
        self.locationManager = [[CLLocationManager alloc] init];
        [[self locationManager] setDelegate:self];
        [[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
        [[self locationManager] setDistanceFilter:10.0];
    }

    [[self locationManager] startUpdatingLocation];
}

第二个函数是 stopUpdate 用于句柄 CLLocationDelegate 以停止更新位置。代码如下所示:

- (void)stopUpdate
{
    if ([self locationManager])
    {
        [[self locationManager] stopUpdatingLocation];
    }
}

所以,对于 CLLocationManagerDelegate 应该是这样的:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{    
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    self.attempts++;

    if(firstPosition == NO)
    {
        if((howRecent < -2.0 || newLocation.horizontalAccuracy > 50.0) && ([self attempts] < 5))
        {
            // force an update, value is not good enough for starting Point            
            [self startUpdates];
            return;
        }
        else
        {
            firstPosition = YES;
            isReadyForReload = YES;
            tempNewLocation = newLocation;
            NSLog(@"## Latitude  : %f", tempNewLocation.coordinate.latitude);
            NSLog(@"## Longitude : %f", tempNewLocation.coordinate.longitude);
            [self stopUpdate];
        }
    }
}

在上面的这个函数中,我只对更新位置的最佳位置是正确的。我希望我的回答会有所帮助,干杯。

于 2012-12-16T13:24:40.587 回答
-1

我认为你的问题的原因是距离过滤器。正如文档所说:

Use the value kCLDistanceFilterNone to be notified of all movements. The default value of this property is kCLDistanceFilterNone. 所以你只拥有你所设置的——持续更新。

于 2012-12-16T12:56:31.853 回答