We get location objects in CLLocationManager’s
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
The issue however is it also gives us some CLLocation objects which are low quality. Since plotting low-quality CLLocation or using them can lead to a bad user experience. We can solve this by using the following properties of CLLocation object
- horizontalAccuracy
- verticalAccuracy
- timestamp
All the accuracy properties are double values and represent distance in meters from the actual location. So a lower value represents a higher accuracy and a negative value represents an invalid location.
- Accuracy value is distance in meters from actual location
- Lower accuracy value means the CLLocation object is closer to actual location
- Higer value means in accurate location value
- Negative accuracy value means garbage data
Keeping all these points in mind we can create a simple function to filter out bad eggs from our CLLocation array
func filterBadLocation(_ location: CLLocation) -> Bool{ let age = -location.timestamp.timeIntervalSinceNow // reject if too old adjust value as per need if age > 10{. return false } // reject if invalid if location.horizontalAccuracy < 0{ return false } // reject if too far from actual location / inaccurate if location.horizontalAccuracy > 100{ return false } // reject if invalid if location.verticalAccuracy < 0{ return false } // reject if too far from actual location / inaccurate if location.verticalAccuracy > 100{ return false } return true }
So basically you are not accepting all you get in delegate method but you are filtering what fits your quality criteria.