gpt4 book ai didi

iphone - CLLocationManager 问题

转载 作者:行者123 更新时间:2023-11-29 04:14:36 25 4
gpt4 key购买 nike

我想要获取单个位置,然后停止从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com