In Swift or Objective-C, we can easily create a CLLocationmanager and start tracking our position. We can also set a distanceFilter, which defines the minimum distance (measured in meters) a device must move horizontally before an update event is generated. My Swift code:
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
manager.distanceFilter = 10
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
But in certain circumstances, with a lower gps accuracy (ie in woods, tunnels, between high buildings...) incorrect spikes are generated. The CLLocationManager lacks a property or method to define a maximum distance for an update in order to clean up this sort of spikes.
Right now, I'm handling the max distance myself storing usable locations into a seperate array:
var myLocations: [CLLocation] = []
func calculateDistanceBetweenTwoLocations(start:CLLocation,destination:CLLocation) {
var distanceInMeters = start.distanceFromLocation(destination)
if distanceInMeters < 20 {
myLocations.append(destination)
}
}
Is there a more accurate way?