Models.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Models.swift
  3. // Runner
  4. //
  5. // Created by Groot on 2025/5/9.
  6. //
  7. import Foundation
  8. import MapKit
  9. extension Decodable {
  10. static func fromJson(json: [String: Any]) -> Self? {
  11. guard let data = try? JSONSerialization.data(withJSONObject: json, options: []) else {
  12. return nil
  13. }
  14. return try? JSONDecoder().decode(Self.self, from: data)
  15. }
  16. }
  17. extension Encodable {
  18. func toJson() -> [String: Any] {
  19. guard let data = try? JSONEncoder().encode(self) else {
  20. return [:]
  21. }
  22. return try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
  23. }
  24. }
  25. struct ATMapCameraPosition: Codable {
  26. var latitude: CGFloat
  27. var longitude: CGFloat
  28. // 地图缩放比例,对应span
  29. var zoom: CGFloat
  30. }
  31. class ATMapPolyline: NSObject, Codable {
  32. var id: String = UUID().uuidString
  33. var points: [CLLocationCoordinate2D]
  34. var polyline: MKPolyline {
  35. return MKPolyline(coordinates: points, count: points.count)
  36. }
  37. init(points: [CLLocationCoordinate2D]) {
  38. self.points = points
  39. }
  40. }
  41. class ATMapMarker: NSObject, Codable {
  42. var id: String
  43. var markerName: String
  44. var latitude: CGFloat
  45. var longitude: CGFloat
  46. var isSelected: Bool
  47. var markerType: any MapMarkerSupportType
  48. enum CodingKeys: String, CodingKey {
  49. case id, markerName, latitude, longitude, isSelected, markerType
  50. }
  51. required init(from decoder: Decoder) throws {
  52. let container = try decoder.container(keyedBy: CodingKeys.self)
  53. id = try container.decode(String.self, forKey: .id)
  54. markerName = try container.decode(String.self, forKey: .markerName)
  55. latitude = try container.decode(CGFloat.self, forKey: .latitude)
  56. longitude = try container.decode(CGFloat.self, forKey: .longitude)
  57. isSelected = try container.decode(Bool.self, forKey: .isSelected)
  58. // 使用工厂方法创建正确类型的markerType
  59. let rawValue = try container.decode(Int.self, forKey: .markerType)
  60. markerType = MarkerTypeFactory.markerType(from: rawValue)
  61. }
  62. func encode(to encoder: Encoder) throws {
  63. var container = encoder.container(keyedBy: CodingKeys.self)
  64. try container.encode(id, forKey: .id)
  65. try container.encode(markerName, forKey: .markerName)
  66. try container.encode(latitude, forKey: .latitude)
  67. try container.encode(longitude, forKey: .longitude)
  68. try container.encode(isSelected, forKey: .isSelected)
  69. try container.encode(markerType.rawValue, forKey: .markerType)
  70. }
  71. }
  72. extension ATMapMarker: MKAnnotation {
  73. var coordinate: CLLocationCoordinate2D {
  74. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  75. }
  76. }
  77. extension CLLocationCoordinate2D: Codable {
  78. enum CodingKeys: String, CodingKey {
  79. case latitude
  80. case longitude
  81. }
  82. public init(from decoder: Decoder) throws {
  83. let container = try decoder.container(keyedBy: CodingKeys.self)
  84. let latitude = try container.decode(Double.self, forKey: .latitude)
  85. let longitude = try container.decode(Double.self, forKey: .longitude)
  86. self.init(latitude: latitude, longitude: longitude)
  87. }
  88. public func encode(to encoder: Encoder) throws {
  89. var container = encoder.container(keyedBy: CodingKeys.self)
  90. try container.encode(latitude, forKey: .latitude)
  91. try container.encode(longitude, forKey: .longitude)
  92. }
  93. }