Guest User

Untitled

a guest
Jan 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. extension CLLocation: Encodable {
  2. public enum CodingKeys: String, CodingKey {
  3. case latitude
  4. case longitude
  5. case altitude
  6. case horizontalAccuracy
  7. case verticalAccuracy
  8. case speed
  9. case course
  10. case timestamp
  11. }
  12. public func encode(to encoder: Encoder) throws {
  13. var container = encoder.container(keyedBy: CodingKeys.self)
  14. try container.encode(coordinate.latitude, forKey: .latitude)
  15. try container.encode(coordinate.longitude, forKey: .longitude)
  16. try container.encode(altitude, forKey: .altitude)
  17. try container.encode(horizontalAccuracy, forKey: .horizontalAccuracy)
  18. try container.encode(verticalAccuracy, forKey: .verticalAccuracy)
  19. try container.encode(speed, forKey: .speed)
  20. try container.encode(course, forKey: .course)
  21. try container.encode(timestamp, forKey: .timestamp)
  22. }
  23. }
  24.  
  25. public struct LocationWrapper: Decodable {
  26. var location: CLLocation
  27.  
  28. init(location: CLLocation) {
  29. self.location = location
  30. }
  31.  
  32. public init(from decoder: Decoder) throws {
  33. let container = try decoder.container(keyedBy: CLLocation.CodingKeys.self)
  34.  
  35. let latitude = try container.decode(CLLocationDegrees.self, forKey: .latitude)
  36. let longitude = try container.decode(CLLocationDegrees.self, forKey: .longitude)
  37. let altitude = try container.decode(CLLocationDistance.self, forKey: .altitude)
  38. let horizontalAccuracy = try container.decode(CLLocationAccuracy.self, forKey: .horizontalAccuracy)
  39. let verticalAccuracy = try container.decode(CLLocationAccuracy.self, forKey: .verticalAccuracy)
  40. let speed = try container.decode(CLLocationSpeed.self, forKey: .speed)
  41. let course = try container.decode(CLLocationDirection.self, forKey: .course)
  42. let timestamp = try container.decode(Date.self, forKey: .timestamp)
  43.  
  44. let location = CLLocation(coordinate: CLLocationCoordinate2DMake(latitude, longitude), altitude: altitude, horizontalAccuracy: horizontalAccuracy, verticalAccuracy: verticalAccuracy, course: course, speed: speed, timestamp: timestamp)
  45.  
  46. self.init(location: location)
  47. }
  48. }
Add Comment
Please, Sign In to add comment