Models.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. lineId = try container.decode(String.self, forKey: .lineId)
  60. lineType = try container.decode(String.self, forKey: .lineType)
  61. points = try container.decode([CLLocationCoordinate2D].self, forKey: .points)
  62. color = try container.decode(String.self, forKey:.color)
  63. width = try container.decode(Double.self, forKey: .width)
  64. if container.contains(.mapPadding) {
  65. mapPadding = try container.decodeIfPresent(ATMapPadding.self, forKey: .mapPadding)
  66. }
  67. }
  68. func encode(to encoder: Encoder) throws {
  69. var container = encoder.container(keyedBy: CodingKeys.self)
  70. try container.encode(lineId, forKey: .lineId)
  71. try container.encode(lineType, forKey: .lineType)
  72. try container.encode(points, forKey: .points)
  73. try container.encodeIfPresent(mapPadding, forKey: .mapPadding)
  74. }
  75. // 初始化方法
  76. init(lineId: String, lineType: String, points: [CLLocationCoordinate2D], mapPadding: ATMapPadding? = nil,color : String,width : Double) {
  77. self.lineId = lineId
  78. self.lineType = lineType
  79. self.points = points
  80. self.mapPadding = mapPadding
  81. self.color = color
  82. self.width = width
  83. super.init()
  84. }
  85. }
  86. class ATMapMarker: NSObject, Codable {
  87. var id: String
  88. var markerName: String?
  89. var customAvatarUrl : String?
  90. var latitude: CGFloat
  91. var longitude: CGFloat
  92. var isSelected: Bool
  93. var markerType: any MapMarkerSupportType
  94. var tags: [String : String]?
  95. init(id: String, markerName: String?, customAvatarUrl: String?,location: CLLocationCoordinate2D, markerType: any MapMarkerSupportType, isSelected: Bool = false,tags: [String : String]?) {
  96. self.id = id
  97. self.markerName = markerName
  98. self.customAvatarUrl = customAvatarUrl
  99. self.latitude = location.latitude
  100. self.longitude = location.longitude
  101. self.markerType = markerType
  102. self.isSelected = isSelected
  103. self.tags = tags
  104. }
  105. func update(coordinate: CLLocationCoordinate2D, name: String? = nil, isSelect: Bool) {
  106. self.latitude = coordinate.latitude
  107. self.longitude = coordinate.longitude
  108. self.markerName = name
  109. self.isSelected = isSelect
  110. }
  111. enum CodingKeys: String, CodingKey {
  112. case id, markerName,customAvatarUrl, latitude, longitude, isSelected, markerType,tags
  113. }
  114. required init(from decoder: Decoder) throws {
  115. let container = try decoder.container(keyedBy: CodingKeys.self)
  116. id = try container.decode(String.self, forKey: .id)
  117. if id.isEmpty {
  118. id = "user_location"
  119. }
  120. if container.contains(.markerName) {
  121. markerName = try container.decodeIfPresent(String.self, forKey: .markerName)
  122. }
  123. if container.contains(.customAvatarUrl) {
  124. customAvatarUrl = try container.decodeIfPresent(String.self, forKey: .customAvatarUrl)
  125. }
  126. latitude = try container.decode(CGFloat.self, forKey: .latitude)
  127. longitude = try container.decode(CGFloat.self, forKey: .longitude)
  128. isSelected = try container.decode(Bool.self, forKey: .isSelected)
  129. // 使用工厂方法创建正确类型的markerType
  130. let rawValue = try container.decode(Int.self, forKey: .markerType)
  131. markerType = MarkerTypeFactory.markerType(from: rawValue)
  132. ///气泡内容
  133. // 处理tags为空的情况
  134. if container.contains(.tags) {
  135. tags = try container.decodeIfPresent([String: String].self, forKey: .tags) ?? [:]
  136. } else {
  137. tags = [:] // 默认为空字典
  138. }
  139. }
  140. func encode(to encoder: Encoder) throws {
  141. var container = encoder.container(keyedBy: CodingKeys.self)
  142. try container.encode(id, forKey: .id)
  143. try container.encode(markerName, forKey: .markerName)
  144. try container.encode(customAvatarUrl, forKey: .customAvatarUrl)
  145. try container.encode(latitude, forKey: .latitude)
  146. try container.encode(longitude, forKey: .longitude)
  147. try container.encode(isSelected, forKey: .isSelected)
  148. try container.encode(markerType.rawValue, forKey: .markerType)
  149. try container.encode(tags, forKey: .tags)
  150. }
  151. }
  152. extension ATMapMarker: MKAnnotation {
  153. var coordinate: CLLocationCoordinate2D {
  154. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  155. }
  156. }
  157. extension CLLocationCoordinate2D: Codable {
  158. enum CodingKeys: String, CodingKey {
  159. case latitude
  160. case longitude
  161. }
  162. public init(from decoder: Decoder) throws {
  163. let container = try decoder.container(keyedBy: CodingKeys.self)
  164. let latitude = try container.decode(Double.self, forKey: .latitude)
  165. let longitude = try container.decode(Double.self, forKey: .longitude)
  166. self.init(latitude: latitude, longitude: longitude)
  167. }
  168. public func encode(to encoder: Encoder) throws {
  169. var container = encoder.container(keyedBy: CodingKeys.self)
  170. try container.encode(latitude, forKey: .latitude)
  171. try container.encode(longitude, forKey: .longitude)
  172. }
  173. }
  174. class ATMapLocation: NSObject, Codable {
  175. var latitude: CGFloat?
  176. var longitude: CGFloat?
  177. var address: String?
  178. var errorCode: Int = 0
  179. var time: Int?
  180. var bearing: CGFloat?
  181. var speed: CGFloat?
  182. init(latitude: CGFloat? = nil, longitude: CGFloat? = nil, address: String? = nil, time: Int? = nil, bearing: CGFloat? = nil, speed: CGFloat? = nil) {
  183. self.latitude = latitude
  184. self.longitude = longitude
  185. self.address = address
  186. self.time = time
  187. self.bearing = bearing
  188. self.speed = speed
  189. super.init()
  190. }
  191. static func fromLocation(location: CLLocation) -> ATMapLocation {
  192. return ATMapLocation(
  193. latitude: CGFloat(location.coordinate.latitude),
  194. longitude: CGFloat(location.coordinate.longitude),
  195. address: nil,
  196. time: Int(location.timestamp.timeIntervalSince1970 * 1000),
  197. bearing: CGFloat(location.course),
  198. speed: CGFloat(location.speed)
  199. )
  200. }
  201. }