我有一个启动CLLocationManager实例的类。目的是使用它来获得一个体面的,一次性修复应用程序启动,然后停止位置服务持续时间(或直到满足一些其他条件需要一个新的修复...但该部分尚未编写)。
出于某种原因,即使我调用[stopUpdatingLocation],我的应用似乎仍然无限期地保持位置服务的活动,无论是否在后台。我的代理人不再按预期收到更新,但箭头仍保留在状态栏上。我关闭所有其他应用程序的位置服务,以验证我的罪魁祸首,然后手动杀死我的应用程序(立即解除了箭头)。
我的代码基于您在Apple文档中找到的内容,并为我的目的添加了一些内容。我已经阅读了所有相关的Apple文档,并且无法弄清楚我做错了什么。关于这个主题的所有其他答案都解决了使用MKMapView而忘记将“showUserLocation”设置为NO的人...我根本没有使用MKMapView,所以这不是我的问题。有没有搞错。为什么不死?
(_ locationManager当然是我的CLLocationManager实例)。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray*)locations
{
CLLocation* lastLocation = [locations lastObject];
// Ignore old (cached) location
NSDate *date = lastLocation.timestamp;
NSTimeInterval howRecent = [date timeIntervalSinceNow];
if (abs(howRecent) > 30.0)
{
DLog(@"Ignoring cached fix");
return;
}
// Wait for better fix if we have GPS
if (lastLocation.horizontalAccuracy > 60 && _GPSEnabled)
{
DLog(@"Ignoring inaccurate fix on GPS-enabled device");
return;
}
// Accept mediocre fix if we don't have GPS
if (lastLocation.horizontalAccuracy < 300 && _GPSEnabled == NO)
{
_hasPos = YES;
_lastPos = lastLocation.coordinate;
DLog(@"Best fix we're likely to get without GPS: %f %f", _lastPos.latitude,
_lastPos.longitude);
[self stopTracking];
return;
}
// If we have GPS, accept a fix with <60m accuracy
_hasPos = YES;
_lastPos = lastLocation.coordinate;
DLog(@"Geolocator has good fix: %f %f", _lastPos.latitude, _lastPos.longitude);
[self stopTracking];
}
-(void)stopTracking
{
DLog(@"Stopped updating locations");
[_locationManager stopUpdatingLocation];
_locationManager.delegate = nil;
}
答案 0 :(得分:0)
最后在另一个问题中找到了答案......在这里重印它是因为我花了一段时间才绊倒它。
我不得不称之为:
[_locationManager stopMonitoringSignificantLocationChanges];
即使我从来没有在第一时间调用startMonitoringSignificantLocationChanges,但似乎我必须“取消调用”它......奇怪的是它以这种方式工作,但是一旦我添加了该行,位置服务就会立即关闭。希望这有助于其他人。