Models.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 lineId: String // 新增:用于记录的线ID
  46. var lineType: String // 新增:用于记录线的样式 normal error selected color
  47. var points: [CLLocationCoordinate2D]
  48. var mapPadding: ATMapPadding?
  49. var color : String ///轨迹的颜色
  50. var width: Double /// 轨迹的宽度
  51. var polyline: MKPolyline {
  52. return MKPolyline(coordinates: points, count: points.count)
  53. }
  54. enum CodingKeys: String, CodingKey {
  55. case lineId, lineType, points, mapPadding,color,width
  56. }
  57. required init(from decoder: Decoder) throws {
  58. let container = try decoder.container(keyedBy: CodingKeys.self)
  59. if container.contains(.lineId) {
  60. lineId = try container.decodeIfPresent(String.self, forKey: .lineId) ?? ""
  61. } else {
  62. lineId = ""
  63. }
  64. if container.contains(.lineType) {
  65. lineType = try container.decodeIfPresent(String.self, forKey: .lineType) ?? ""
  66. } else {
  67. lineType = "";
  68. }
  69. if container.contains(.points) {
  70. points = try container.decodeIfPresent([CLLocationCoordinate2D].self, forKey: .points) ?? [CLLocationCoordinate2D]()
  71. } else {
  72. points = [CLLocationCoordinate2D]()
  73. }
  74. if container.contains(.color) {
  75. color = try container.decodeIfPresent(String.self, forKey: .color) ?? ""
  76. } else {
  77. color = ""
  78. }
  79. if container.contains(.width) {
  80. width = try container.decodeIfPresent(Double.self, forKey: .width) ?? 0
  81. } else {
  82. width = 0
  83. }
  84. if container.contains(.mapPadding) {
  85. mapPadding = try container.decodeIfPresent(ATMapPadding.self, forKey: .mapPadding) ?? ATMapPadding(top: 0, left: 0, bottom: 0, right: 0)
  86. } else {
  87. mapPadding = ATMapPadding(top: 0, left: 0, bottom: 0, right: 0)
  88. }
  89. }
  90. func encode(to encoder: Encoder) throws {
  91. var container = encoder.container(keyedBy: CodingKeys.self)
  92. try container.encode(lineId, forKey: .lineId)
  93. try container.encode(lineType, forKey: .lineType)
  94. try container.encode(points, forKey: .points)
  95. try container.encodeIfPresent(mapPadding, forKey: .mapPadding)
  96. }
  97. // 初始化方法
  98. init(lineId: String, lineType: String, points: [CLLocationCoordinate2D], mapPadding: ATMapPadding? = nil,color : String,width : Double) {
  99. self.lineId = lineId
  100. self.lineType = lineType
  101. self.points = points
  102. self.mapPadding = mapPadding
  103. self.color = color
  104. self.width = width
  105. super.init()
  106. }
  107. }
  108. class ATMapMarker: NSObject, Codable {
  109. var id: String
  110. var markerId: String
  111. var markerName: String?
  112. var customAvatarUrl : String?
  113. var latitude: CGFloat
  114. var longitude: CGFloat
  115. var isSelected: Bool
  116. var markerType: any MapMarkerSupportType
  117. var tags: [String : String]?
  118. init(id: String, markerId: String,markerName: String?, customAvatarUrl: String?,location: CLLocationCoordinate2D, markerType: any MapMarkerSupportType, isSelected: Bool = false,tags: [String : String]?) {
  119. self.id = id
  120. self.markerId = markerId
  121. self.markerName = markerName
  122. self.customAvatarUrl = customAvatarUrl
  123. self.latitude = location.latitude
  124. self.longitude = location.longitude
  125. self.markerType = markerType
  126. self.isSelected = isSelected
  127. self.tags = tags
  128. }
  129. func update(coordinate: CLLocationCoordinate2D, name: String? = nil, isSelect: Bool) {
  130. self.latitude = coordinate.latitude
  131. self.longitude = coordinate.longitude
  132. self.markerName = name
  133. self.isSelected = isSelect
  134. }
  135. enum CodingKeys: String, CodingKey {
  136. case id,markerId,markerName,customAvatarUrl, latitude, longitude, isSelected, markerType,tags
  137. }
  138. required init(from decoder: Decoder) throws {
  139. let container = try decoder.container(keyedBy: CodingKeys.self)
  140. id = try container.decode(String.self, forKey: .id)
  141. if id.isEmpty {
  142. id = "user_location"
  143. }
  144. if container.contains(.markerName) {
  145. markerName = try container.decodeIfPresent(String.self, forKey: .markerName) ?? ""
  146. } else {
  147. markerName = ""
  148. }
  149. if container.contains(.customAvatarUrl) {
  150. customAvatarUrl = try container.decodeIfPresent(String.self, forKey: .customAvatarUrl) ?? ""
  151. } else {
  152. customAvatarUrl = ""
  153. }
  154. if container.contains(.latitude) {
  155. latitude = try container.decodeIfPresent(CGFloat.self, forKey: .latitude) ?? 0
  156. } else {
  157. latitude = 0
  158. }
  159. if container.contains(.longitude) {
  160. longitude = try container.decodeIfPresent(CGFloat.self, forKey: .longitude) ?? 0
  161. } else {
  162. longitude = 0
  163. }
  164. if container.contains(.isSelected) {
  165. isSelected = try container.decodeIfPresent(Bool.self, forKey: .isSelected) ?? false
  166. } else {
  167. isSelected = false
  168. }
  169. // 使用工厂方法创建正确类型的markerType
  170. if container.contains(.markerType) {
  171. let rawValue = try container.decodeIfPresent(Int.self, forKey: .markerType) ?? 1
  172. markerType = MarkerTypeFactory.markerType(from: rawValue)
  173. } else {
  174. markerType = MarkerTypeFactory.markerType(from: 1)
  175. }
  176. ///气泡内容
  177. // 处理tags为空的情况
  178. if container.contains(.tags) {
  179. tags = try container.decodeIfPresent([String: String].self, forKey: .tags) ?? [:]
  180. } else {
  181. tags = [:] // 默认为空字典
  182. }
  183. if container.contains(.markerId) {
  184. markerId = try container.decodeIfPresent(String.self, forKey: .markerId) ?? ""
  185. } else {
  186. markerId = ""
  187. }
  188. }
  189. func encode(to encoder: Encoder) throws {
  190. var container = encoder.container(keyedBy: CodingKeys.self)
  191. try container.encode(id, forKey: .id)
  192. try container.encode(markerName, forKey: .markerName)
  193. try container.encode(customAvatarUrl, forKey: .customAvatarUrl)
  194. try container.encode(latitude, forKey: .latitude)
  195. try container.encode(longitude, forKey: .longitude)
  196. try container.encode(isSelected, forKey: .isSelected)
  197. try container.encode(markerType.rawValue, forKey: .markerType)
  198. try container.encode(tags, forKey: .tags)
  199. try container.encode(markerId, forKey: .markerId)
  200. }
  201. }
  202. extension ATMapMarker: MKAnnotation {
  203. var coordinate: CLLocationCoordinate2D {
  204. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  205. }
  206. }
  207. extension CLLocationCoordinate2D: Codable {
  208. enum CodingKeys: String, CodingKey {
  209. case latitude
  210. case longitude
  211. }
  212. public init(from decoder: Decoder) throws {
  213. let container = try decoder.container(keyedBy: CodingKeys.self)
  214. let latitude = try container.decode(Double.self, forKey: .latitude)
  215. let longitude = try container.decode(Double.self, forKey: .longitude)
  216. self.init(latitude: latitude, longitude: longitude)
  217. }
  218. public func encode(to encoder: Encoder) throws {
  219. var container = encoder.container(keyedBy: CodingKeys.self)
  220. try container.encode(latitude, forKey: .latitude)
  221. try container.encode(longitude, forKey: .longitude)
  222. }
  223. }
  224. class ATMapLocation: NSObject, Codable {
  225. var latitude: CGFloat?
  226. var longitude: CGFloat?
  227. var address: String?
  228. var errorCode: Int = 0
  229. var time: Int?
  230. var bearing: CGFloat?
  231. var speed: CGFloat?
  232. init(latitude: CGFloat? = nil, longitude: CGFloat? = nil, address: String? = nil, time: Int? = nil, bearing: CGFloat? = nil, speed: CGFloat? = nil) {
  233. self.latitude = latitude
  234. self.longitude = longitude
  235. self.address = address
  236. self.time = time
  237. self.bearing = bearing
  238. self.speed = speed
  239. super.init()
  240. }
  241. static func fromLocation(location: CLLocation) -> ATMapLocation {
  242. return ATMapLocation(
  243. latitude: CGFloat(location.coordinate.latitude),
  244. longitude: CGFloat(location.coordinate.longitude),
  245. address: nil,
  246. time: Int(location.timestamp.timeIntervalSince1970 * 1000),
  247. bearing: CGFloat(location.course),
  248. speed: CGFloat(location.speed)
  249. )
  250. }
  251. }