Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created September 16, 2017 01:05
Show Gist options
  • Save jakehawken/0457582fdcabd5eeb4de1aa7686441f9 to your computer and use it in GitHub Desktop.
Save jakehawken/0457582fdcabd5eeb4de1aa7686441f9 to your computer and use it in GitHub Desktop.
import CoreLocation
extension CLLocation {
public static func +(lhs: CLLocation, rhs: CLLocation) -> CLLocation {
let summedLat = lhs.coordinate.latitude + rhs.coordinate.latitude
let summedLong = lhs.coordinate.longitude + rhs.coordinate.longitude
return CLLocation(latitude: summedLat, longitude: summedLong)
}
public static func /(lhs: CLLocation, rhs: Double) -> CLLocation {
let dividedLat = lhs.coordinate.latitude / rhs
let dividedLong = lhs.coordinate.longitude / rhs
return CLLocation(latitude: dividedLat, longitude: dividedLong)
}
public static func +=(left: inout CLLocation, right: CLLocation) {
left = left + right
}
}
class LocationSmoother {
private(set) var lastLocation: CLLocation?
func smooth(_ loc: CLLocation) -> CLLocation {
if let last = lastLocation {
lastLocation = (last + loc) / 2
}
else {
lastLocation = loc
}
return lastLocation!
}
}
let loc1 = CLLocation(latitude: 438, longitude: 236)
let loc2 = CLLocation(latitude: 439, longitude: 237)
let loc3 = CLLocation(latitude: 440, longitude: 238)
let loc4 = CLLocation(latitude: 450, longitude: 250)
let loc5 = CLLocation(latitude: 442, longitude: 240)
let loc6 = CLLocation(latitude: 444, longitude: 242)
let smoother = LocationSmoother()
print("Actual location: \(loc1.coordinate) \nSmoothed location: \(smoother.smooth(loc1).coordinate)\n")
print("Actual location: \(loc2.coordinate) \nSmoothed location: \(smoother.smooth(loc2).coordinate)\n")
print("Actual location: \(loc3.coordinate) \nSmoothed location: \(smoother.smooth(loc3).coordinate)\n")
print("Actual location: \(loc4.coordinate) \nSmoothed location: \(smoother.smooth(loc4).coordinate)\n")
print("Actual location: \(loc5.coordinate) \nSmoothed location: \(smoother.smooth(loc5).coordinate)\n")
print("Actual location: \(loc6.coordinate) \nSmoothed location: \(smoother.smooth(loc6).coordinate)\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment