我遇到了CLLocationManager的一些奇怪问题。我的应用程序在位置更新时起作用,当用户移动大约250米时,有一些事情要做。
我必须向用户展示他们彼此之间的距离,因为我需要每隔5米更新一次位置。我有时会收到相距300米到400米的位置更新。这主要发生在我第一次运行应用程序时。但是当爆发中抛出最后一次位置更新时,它有点准确。
我正在检查CLLocation的时间戳和水平准确性,但这无济于事。如何删除这些不需要的位置更新?
+(instancetype)sharedInstance {
static id sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[LocationManager alloc]init];
});
return sharedInstance;
}
#pragma mark - Get User Location
-(void)getUserLocation{
if (self.locationManager==nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 5;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.allowsBackgroundLocationUpdates = YES;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[self.locationManager startMonitoringSignificantLocationChanges];
if([[UIDevice currentDevice].systemVersion floatValue] >=8.0) {
// [locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 5.0 && manager.desiredAccuracy == kCLLocationAccuracyBest && location.horizontalAccuracy<=10) {
// If the event is recent, do something with it.
NSLog(@"New Event");
NSLog(@"latitude %+.6f, longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
[[ParseManagerClass sharedInstance]saveUserLocation:location];
}else{
NSLog(@"cached one event");
}
}