Models.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. init(id: String, markerName: String?, customAvatarUrl: String?,location: CLLocationCoordinate2D, markerType: any MapMarkerSupportType, isSelected: Bool = false) {
  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. }
  104. func update(coordinate: CLLocationCoordinate2D, name: String? = nil, isSelect: Bool) {
  105. self.latitude = coordinate.latitude
  106. self.longitude = coordinate.longitude
  107. self.markerName = name
  108. self.isSelected = isSelect
  109. }
  110. enum CodingKeys: String, CodingKey {
  111. case id, markerName,customAvatarUrl, latitude, longitude, isSelected, markerType
  112. }
  113. required init(from decoder: Decoder) throws {
  114. let container = try decoder.container(keyedBy: CodingKeys.self)
  115. id = try container.decode(String.self, forKey: .id)
  116. if id.isEmpty {
  117. id = "user_location"
  118. }
  119. if container.contains(.markerName) {
  120. markerName = try container.decodeIfPresent(String.self, forKey: .markerName)
  121. }
  122. if container.contains(.customAvatarUrl) {
  123. customAvatarUrl = try container.decodeIfPresent(String.self, forKey: .customAvatarUrl)
  124. }
  125. latitude = try container.decode(CGFloat.self, forKey: .latitude)
  126. longitude = try container.decode(CGFloat.self, forKey: .longitude)
  127. isSelected = try container.decode(Bool.self, forKey: .isSelected)
  128. // 使用工厂方法创建正确类型的markerType
  129. let rawValue = try container.decode(Int.self, forKey: .markerType)
  130. markerType = MarkerTypeFactory.markerType(from: rawValue)
  131. }
  132. func encode(to encoder: Encoder) throws {
  133. var container = encoder.container(keyedBy: CodingKeys.self)
  134. try container.encode(id, forKey: .id)
  135. try container.encode(markerName, forKey: .markerName)
  136. try container.encode(customAvatarUrl, forKey: .customAvatarUrl)
  137. try container.encode(latitude, forKey: .latitude)
  138. try container.encode(longitude, forKey: .longitude)
  139. try container.encode(isSelected, forKey: .isSelected)
  140. try container.encode(markerType.rawValue, forKey: .markerType)
  141. }
  142. }
  143. extension ATMapMarker: MKAnnotation {
  144. var coordinate: CLLocationCoordinate2D {
  145. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  146. }
  147. }
  148. extension CLLocationCoordinate2D: Codable {
  149. enum CodingKeys: String, CodingKey {
  150. case latitude
  151. case longitude
  152. }
  153. public init(from decoder: Decoder) throws {
  154. let container = try decoder.container(keyedBy: CodingKeys.self)
  155. let latitude = try container.decode(Double.self, forKey: .latitude)
  156. let longitude = try container.decode(Double.self, forKey: .longitude)
  157. self.init(latitude: latitude, longitude: longitude)
  158. }
  159. public func encode(to encoder: Encoder) throws {
  160. var container = encoder.container(keyedBy: CodingKeys.self)
  161. try container.encode(latitude, forKey: .latitude)
  162. try container.encode(longitude, forKey: .longitude)
  163. }
  164. }
  165. class ATMapLocation: NSObject, Codable {
  166. var latitude: CGFloat?
  167. var longitude: CGFloat?
  168. var address: String?
  169. var errorCode: Int = 0
  170. var time: Int?
  171. var bearing: CGFloat?
  172. var speed: CGFloat?
  173. init(latitude: CGFloat? = nil, longitude: CGFloat? = nil, address: String? = nil, time: Int? = nil, bearing: CGFloat? = nil, speed: CGFloat? = nil) {
  174. self.latitude = latitude
  175. self.longitude = longitude
  176. self.address = address
  177. self.time = time
  178. self.bearing = bearing
  179. self.speed = speed
  180. super.init()
  181. }
  182. static func fromLocation(location: CLLocation) -> ATMapLocation {
  183. return ATMapLocation(
  184. latitude: CGFloat(location.coordinate.latitude),
  185. longitude: CGFloat(location.coordinate.longitude),
  186. address: nil,
  187. time: Int(location.timestamp.timeIntervalSince1970 * 1000),
  188. bearing: CGFloat(location.course),
  189. speed: CGFloat(location.speed)
  190. )
  191. }
  192. }