Models.swift 6.5 KB

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