根据Apple Docs的说法,位置发生重大变化应该至少每15分钟更新一次位置。我的举动确实很大,但设备静止不动时却收到更新。您有什么更新经验?他们至少每15分钟来一次吗?
如果GPS级别的精度对您的应用不是很关键,并且您不需要连续跟踪,则可以使用重大变化的位置服务。正确使用重要更改位置服务至关重要,因为即使没有发生位置更改,它也至少每15分钟唤醒一次系统和您的应用,并且该服务会连续运行直到您停止它为止。
好吧,我有一个幼稚的解决方案。您可以使用NSTimer强制CLLocationManger实例每15分钟或您希望其定期更新的时间更新一次。
这是我要使用的代码:
首先,只要需要在viewDidLoad或其他位置,调用此方法即可开始更新位置。
- (void)startStandardUpdates
{
if (nil == locationManager){
locationManager = [[CLLocationManager alloc] init];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 900 seconds is equal to 15 minutes
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:900 target:self selector:@selector(updateUserLocation) userInfo:nil repeats:YES];
[timer fire];
}
其次,实现updateUserLocation方法:
-(void)updateUserLocation{
[self.locationManager startUpdatingLocation];
}
最后,确认协议,然后执行位置更新方法。我们读取了最新的更新结果,并让位置管理器停止更新当前位置,直到接下来的15分钟。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *userLocation = [locations objectAtIndex:0];
CLLocationCoordinate2D userLocationCoordinate = userLocation.coordinate;
/*
Do whatever you want to update by using the updated userLocationCoordinate.
*/
[manager stopUpdatingLocation];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句