Models.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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: Any) -> Self? {
  11. do {
  12. let data = try JSONSerialization.data(withJSONObject: json, options: [])
  13. return try JSONDecoder().decode(Self.self, from: data)
  14. } catch {
  15. print(error.localizedDescription)
  16. return nil
  17. }
  18. }
  19. }
  20. extension Encodable {
  21. func toJson() -> [String: Any] {
  22. guard let data = try? JSONEncoder().encode(self) else {
  23. return [:]
  24. }
  25. return try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
  26. }
  27. }
  28. struct ATMapCameraPosition: Codable {
  29. var latitude: CGFloat
  30. var longitude: CGFloat
  31. // 地图缩放比例,对应span
  32. var zoom: CGFloat
  33. }
  34. struct ATMapPadding: Codable {
  35. var top: CGFloat
  36. var left: CGFloat
  37. var bottom: CGFloat
  38. var right: CGFloat
  39. var padding: UIEdgeInsets {
  40. return UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
  41. }
  42. }
  43. class ATMapPolyline: NSObject, Codable {
  44. var id: String = UUID().uuidString
  45. var points: [CLLocationCoordinate2D]
  46. var mapPadding: ATMapPadding?
  47. var polyline: MKPolyline {
  48. return MKPolyline(coordinates: points, count: points.count)
  49. }
  50. enum CodingKeys: String, CodingKey {
  51. case points, mapPadding
  52. }
  53. required init(from decoder: Decoder) throws {
  54. let container = try decoder.container(keyedBy: CodingKeys.self)
  55. points = try container.decode([CLLocationCoordinate2D].self, forKey: .points)
  56. if container.contains(.mapPadding) {
  57. mapPadding = try container.decodeIfPresent(ATMapPadding.self, forKey: .mapPadding)
  58. }
  59. }
  60. func encode(to encoder: Encoder) throws {
  61. var container = encoder.container(keyedBy: CodingKeys.self)
  62. try container.encode(points, forKey: .points)
  63. try container.encodeIfPresent(mapPadding, forKey: .mapPadding)
  64. }
  65. }
  66. class ATMapMarker: NSObject, Codable {
  67. var id: String
  68. var markerName: String?
  69. var latitude: CGFloat
  70. var longitude: CGFloat
  71. var isSelected: Bool
  72. var markerType: any MapMarkerSupportType
  73. init(id: String, markerName: String?, location: CLLocationCoordinate2D, markerType: any MapMarkerSupportType, isSelected: Bool = false) {
  74. self.id = id
  75. self.markerName = markerName
  76. self.latitude = location.latitude
  77. self.longitude = location.longitude
  78. self.markerType = markerType
  79. self.isSelected = isSelected
  80. }
  81. func update(coordinate: CLLocationCoordinate2D, name: String? = nil, isSelect: Bool) {
  82. self.latitude = coordinate.latitude
  83. self.longitude = coordinate.longitude
  84. self.markerName = name
  85. self.isSelected = isSelect
  86. }
  87. enum CodingKeys: String, CodingKey {
  88. case id, markerName, latitude, longitude, isSelected, markerType
  89. }
  90. required init(from decoder: Decoder) throws {
  91. let container = try decoder.container(keyedBy: CodingKeys.self)
  92. id = try container.decode(String.self, forKey: .id)
  93. if container.contains(.markerName) {
  94. markerName = try container.decodeIfPresent(String.self, forKey: .markerName)
  95. }
  96. latitude = try container.decode(CGFloat.self, forKey: .latitude)
  97. longitude = try container.decode(CGFloat.self, forKey: .longitude)
  98. isSelected = try container.decode(Bool.self, forKey: .isSelected)
  99. // 使用工厂方法创建正确类型的markerType
  100. let rawValue = try container.decode(Int.self, forKey: .markerType)
  101. markerType = MarkerTypeFactory.markerType(from: rawValue)
  102. }
  103. func encode(to encoder: Encoder) throws {
  104. var container = encoder.container(keyedBy: CodingKeys.self)
  105. try container.encode(id, forKey: .id)
  106. try container.encode(markerName, forKey: .markerName)
  107. try container.encode(latitude, forKey: .latitude)
  108. try container.encode(longitude, forKey: .longitude)
  109. try container.encode(isSelected, forKey: .isSelected)
  110. try container.encode(markerType.rawValue, forKey: .markerType)
  111. }
  112. }
  113. extension ATMapMarker: MKAnnotation {
  114. var coordinate: CLLocationCoordinate2D {
  115. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  116. }
  117. }
  118. extension CLLocationCoordinate2D: Codable {
  119. enum CodingKeys: String, CodingKey {
  120. case latitude
  121. case longitude
  122. }
  123. public init(from decoder: Decoder) throws {
  124. let container = try decoder.container(keyedBy: CodingKeys.self)
  125. let latitude = try container.decode(Double.self, forKey: .latitude)
  126. let longitude = try container.decode(Double.self, forKey: .longitude)
  127. self.init(latitude: latitude, longitude: longitude)
  128. }
  129. public func encode(to encoder: Encoder) throws {
  130. var container = encoder.container(keyedBy: CodingKeys.self)
  131. try container.encode(latitude, forKey: .latitude)
  132. try container.encode(longitude, forKey: .longitude)
  133. }
  134. }