我想获得一个位置,然后停止从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:
中我仍然收到通知,知道为什么会发生吗?
最佳答案
也许尝试我的解决方案。我正在构建两个用于处理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];
}
}
}
在上面的此函数中,我仅对更新位置的最佳位置进行了纠正。希望我的回答会有所帮助,干杯。
关于iphone - CLLocationManager问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13900829/