Models.swift 7.6 KB

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