Created
January 16, 2019 23:57
-
-
Save hishma/7be2361888859e94cd0a898bb33c1383 to your computer and use it in GitHub Desktop.
CoreLocation and Codable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CLLocation: Encodable { | |
public enum CodingKeys: String, CodingKey { | |
case latitude | |
case longitude | |
case altitude | |
case horizontalAccuracy | |
case verticalAccuracy | |
case speed | |
case course | |
case timestamp | |
} | |
public func encode(to encoder: Encoder) throws { | |
var container = encoder.container(keyedBy: CodingKeys.self) | |
try container.encode(coordinate.latitude, forKey: .latitude) | |
try container.encode(coordinate.longitude, forKey: .longitude) | |
try container.encode(altitude, forKey: .altitude) | |
try container.encode(horizontalAccuracy, forKey: .horizontalAccuracy) | |
try container.encode(verticalAccuracy, forKey: .verticalAccuracy) | |
try container.encode(speed, forKey: .speed) | |
try container.encode(course, forKey: .course) | |
try container.encode(timestamp, forKey: .timestamp) | |
} | |
} | |
public struct LocationWrapper: Decodable { | |
var location: CLLocation | |
init(location: CLLocation) { | |
self.location = location | |
} | |
public init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: CLLocation.CodingKeys.self) | |
let latitude = try container.decode(CLLocationDegrees.self, forKey: .latitude) | |
let longitude = try container.decode(CLLocationDegrees.self, forKey: .longitude) | |
let altitude = try container.decode(CLLocationDistance.self, forKey: .altitude) | |
let horizontalAccuracy = try container.decode(CLLocationAccuracy.self, forKey: .horizontalAccuracy) | |
let verticalAccuracy = try container.decode(CLLocationAccuracy.self, forKey: .verticalAccuracy) | |
let speed = try container.decode(CLLocationSpeed.self, forKey: .speed) | |
let course = try container.decode(CLLocationDirection.self, forKey: .course) | |
let timestamp = try container.decode(Date.self, forKey: .timestamp) | |
let location = CLLocation(coordinate: CLLocationCoordinate2DMake(latitude, longitude), altitude: altitude, horizontalAccuracy: horizontalAccuracy, verticalAccuracy: verticalAccuracy, course: course, speed: speed, timestamp: timestamp) | |
self.init(location: location) | |
} | |
} |
👍
if let sourceInfoWrapper = sourceInfoWrapper {
location = CLLocation(coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude),
altitude: altitude,
horizontalAccuracy: horizontalAccuracy,
verticalAccuracy: verticalAccuracy,
course: course,
courseAccuracy: courseAccuracy,
speed: speed,
speedAccuracy: speedAccuracy,
timestamp: timestamp,
sourceInfo: sourceInfoWrapper.sourceInfo)
}
@yoni-placer There is a potential error here. If the sourceInfoWrapper
is nil
, the courseAccuracy
and the speedAccuracy
will not be initialized.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For iOS 15: