[英]CLLocationManager issue
我想獲得一個位置,然后停止從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];
}
}
}
在上面的此函數中,我只對更新位置的最佳位置進行了糾正。 希望我的回答會有所幫助,干杯。
我認為您的問題的原因在於距離過濾器。 正如文檔所說:
Use the value kCLDistanceFilterNone to be notified of all movements. The default value of this property is kCLDistanceFilterNone.
這樣,您就可以設置-持續更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.