我想获取单个位置,然后停止接收来自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:中
我仍然会收到通知,你知道为什么会这样吗?
发布于 2012-12-16 21:24:41
也许可以试试我的解决方案。我正在构建两个函数来处理LocationManger对象。第一个函数是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-16 20:56:32
我认为你的问题的原因在于距离过滤器。正如文档所说:
Use the value kCLDistanceFilterNone to be notified of all movements. The default value of this property is kCLDistanceFilterNone.,这样你就有了你设置的东西--连续更新。
https://stackoverflow.com/questions/13900829
复制相似问题