Models.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 id.isEmpty {
  94. id = "user_location"
  95. }
  96. if container.contains(.markerName) {
  97. markerName = try container.decodeIfPresent(String.self, forKey: .markerName)
  98. }
  99. latitude = try container.decode(CGFloat.self, forKey: .latitude)
  100. longitude = try container.decode(CGFloat.self, forKey: .longitude)
  101. isSelected = try container.decode(Bool.self, forKey: .isSelected)
  102. // 使用工厂方法创建正确类型的markerType
  103. let rawValue = try container.decode(Int.self, forKey: .markerType)
  104. markerType = MarkerTypeFactory.markerType(from: rawValue)
  105. }
  106. func encode(to encoder: Encoder) throws {
  107. var container = encoder.container(keyedBy: CodingKeys.self)
  108. try container.encode(id, forKey: .id)
  109. try container.encode(markerName, forKey: .markerName)
  110. try container.encode(latitude, forKey: .latitude)
  111. try container.encode(longitude, forKey: .longitude)
  112. try container.encode(isSelected, forKey: .isSelected)
  113. try container.encode(markerType.rawValue, forKey: .markerType)
  114. }
  115. }
  116. extension ATMapMarker: MKAnnotation {
  117. var coordinate: CLLocationCoordinate2D {
  118. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  119. }
  120. }
  121. extension CLLocationCoordinate2D: Codable {
  122. enum CodingKeys: String, CodingKey {
  123. case latitude
  124. case longitude
  125. }
  126. public init(from decoder: Decoder) throws {
  127. let container = try decoder.container(keyedBy: CodingKeys.self)
  128. let latitude = try container.decode(Double.self, forKey: .latitude)
  129. let longitude = try container.decode(Double.self, forKey: .longitude)
  130. self.init(latitude: latitude, longitude: longitude)
  131. }
  132. public func encode(to encoder: Encoder) throws {
  133. var container = encoder.container(keyedBy: CodingKeys.self)
  134. try container.encode(latitude, forKey: .latitude)
  135. try container.encode(longitude, forKey: .longitude)
  136. }
  137. }
  138. class ATMapLocation: NSObject, Codable {
  139. var latitude: CGFloat?
  140. var longitude: CGFloat?
  141. var address: String?
  142. var errorCode: Int = 0
  143. var time: Int?
  144. var bearing: CGFloat?
  145. var speed: CGFloat?
  146. init(latitude: CGFloat? = nil, longitude: CGFloat? = nil, address: String? = nil, time: Int? = nil, bearing: CGFloat? = nil, speed: CGFloat? = nil) {
  147. self.latitude = latitude
  148. self.longitude = longitude
  149. self.address = address
  150. self.time = time
  151. self.bearing = bearing
  152. self.speed = speed
  153. super.init()
  154. }
  155. static func fromLocation(location: CLLocation) -> ATMapLocation {
  156. return ATMapLocation(
  157. latitude: CGFloat(location.coordinate.latitude),
  158. longitude: CGFloat(location.coordinate.longitude),
  159. address: nil,
  160. time: Int(location.timestamp.timeIntervalSince1970 * 1000),
  161. bearing: CGFloat(location.course),
  162. speed: CGFloat(location.speed)
  163. )
  164. }
  165. }