我在一些应用中使用Location Services
。我有一种方法,我在locationManager:didUpdateToLocation:fromLocation:
方法中使用它来过滤掉不良,不准确或太远的位置。并尽量减少gps“抖动”。这是我使用的:
/**
* Check if we have a valid location
*
* @version $Revision: 0.1
*/
+ (BOOL)isValidLocation:(CLLocation *)newLocation withOldLocation:(CLLocation *)oldLocation {
// Filter out nil locations
if (!newLocation) return NO;
// Filter out points by invalid accuracy
if (newLocation.horizontalAccuracy < 0) return NO;
if (newLocation.horizontalAccuracy > 66) return NO;
// Filter out points by invalid accuracy
#if !TARGET_IPHONE_SIMULATOR
if (newLocation.verticalAccuracy < 0) return NO;
#endif
// Filter out points that are out of order
NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
if (secondsSinceLastPoint < 0) return NO;
// Make sure the update is new not cached
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return NO;
// Check to see if old and new are the same
if ((oldLocation.coordinate.latitude == newLocation.coordinate.latitude) && (oldLocation.coordinate.longitude == newLocation.coordinate.longitude))
return NO;
return YES;
}//end
有没有人对此方法进行任何改进以使其更准确? 66 过高horizontalAccuracy
并且会收到大量无效位置吗?我应该降低这个吗?
有没有办法摆脱iPhone上gps的“抖动”?
答案 0 :(得分:7)
除此之外,还有一个我用的:
if(self.lastKnownLocation)
{
CLLocationDistance dist = [newLocation distanceFromLocation:self.lastKnownLocation];
if(dist > newLocation.horizontalAccuracy)
{
//.....
}
}
self.lastKnownLocation
实际上是我拥有的最后一个有效位置,它是:
@property(nonatomic, copy) CLLocation *lastKnownLocation;
答案 1 :(得分:5)
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (!newLocation)
{
NSLog(@"Filter out points by invalid accuracy");
return;
}
// Filter out points by invalid accuracy
if (newLocation.horizontalAccuracy < 0)
{
return;
}
if(oldLocation.coordinate.latitude>90 && oldLocation.coordinate.latitude<-90 && oldLocation.coordinate.longitude>180 && oldLocation.coordinate.longitude<-180)
{
NSLog(@"old");
return;
}
if(newLocation.coordinate.latitude>90 || newLocation.coordinate.latitude<-90 || newLocation.coordinate.longitude>180 || newLocation.coordinate.longitude<-180)
{
NSLog(@"new");
return;
}
///////
NSDate *eventDate=newLocation.timestamp;
NSTimeInterval eventinterval=[eventDate timeIntervalSinceNow];
if (abs(eventinterval)<30.0)
{
if (newLocation.horizontalAccuracy>=0 && newLocation.horizontalAccuracy<20)
{
**//finally you are getting right updated value here....**
}
}
}
答案 2 :(得分:0)
你可以结帐
主要思想是在方法didUpdateLocation:
过滤方法就像这样:
func filterAndAddLocation(_ location: CLLocation) -> Bool{
let age = -location.timestamp.timeIntervalSinceNow
if age > 10{
return false
}
if location.horizontalAccuracy < 0{
return false
}
if location.horizontalAccuracy > 100{
return false
}
locationDataArray.append(location)
return true
}