Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikKovIos/42808d2ef3831bd3d18171b4a144412e to your computer and use it in GitHub Desktop.
Save NikKovIos/42808d2ef3831bd3d18171b4a144412e to your computer and use it in GitHub Desktop.
CLLocationCoordinate2D Equatable with accuracy
extension CLLocationCoordinate2D: Equatable {
public static func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
let numbersAfterCommaAccuracy: Double = 4
let ratio = numbersAfterCommaAccuracy * 10
let isLatitudeEqual = ((lhs.latitude - rhs.latitude) * ratio).rounded(.down) == 0
let isLongitudeEqual = ((lhs.latitude - rhs.latitude) * ratio).rounded(.down) == 0
return isLatitudeEqual && isLongitudeEqual
}
}
// Tests:
let location1 = CLLocationCoordinate2D(latitude: 55.11925634599045, longitude: 36.60896733330129)
let location2 = CLLocationCoordinate2D(latitude: 55.11925632599047, longitude: 36.608967333301294)
let location3 = CLLocationCoordinate2D(latitude: 55.11925, longitude: 36.60896)
let location4 = CLLocationCoordinate2D(latitude: 55.11926, longitude: 36.60897)
let location5 = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
print(location1 == location2) // true
print(location1 == location3) // true
print(location1 == location4) // false
print(location1 == location5) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment