Models.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: [String: Any]) -> Self? {
  11. guard let data = try? JSONSerialization.data(withJSONObject: json, options: []) else {
  12. return nil
  13. }
  14. return try? JSONDecoder().decode(Self.self, from: data)
  15. }
  16. }
  17. extension Encodable {
  18. func toJson() -> [String: Any] {
  19. guard let data = try? JSONEncoder().encode(self) else {
  20. return [:]
  21. }
  22. return try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
  23. }
  24. }
  25. enum ATMapMarkerType: Int, CaseIterable {
  26. case mine = 1
  27. case friend = 2
  28. case traceStartPoint = 3
  29. case traceEndFriendPoint = 4
  30. case traceEndMinePoint = 5
  31. }
  32. struct ATMapCameraPosition: Codable {
  33. var latitude: CGFloat
  34. var longitude: CGFloat
  35. // 地图缩放比例,对应span
  36. var zoom: CGFloat
  37. }
  38. class ATMapPolyline: NSObject, Codable {
  39. var id: String = UUID().uuidString
  40. var points: [CLLocationCoordinate2D]
  41. var polyline: MKPolyline {
  42. return MKPolyline(coordinates: points, count: points.count)
  43. }
  44. init(points: [CLLocationCoordinate2D]) {
  45. self.points = points
  46. }
  47. }
  48. class ATMapMarker: NSObject, Codable {
  49. var id: String
  50. var markerName: String
  51. var latitude: CGFloat
  52. var longitude: CGFloat
  53. var isSelected: Bool
  54. }
  55. extension ATMapMarker: MKAnnotation {
  56. var coordinate: CLLocationCoordinate2D {
  57. return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  58. }
  59. }
  60. extension CLLocationCoordinate2D: Codable {
  61. enum CodingKeys: String, CodingKey {
  62. case latitude
  63. case longitude
  64. }
  65. public init(from decoder: Decoder) throws {
  66. let container = try decoder.container(keyedBy: CodingKeys.self)
  67. let latitude = try container.decode(Double.self, forKey: .latitude)
  68. let longitude = try container.decode(Double.self, forKey: .longitude)
  69. self.init(latitude: latitude, longitude: longitude)
  70. }
  71. public func encode(to encoder: Encoder) throws {
  72. var container = encoder.container(keyedBy: CodingKeys.self)
  73. try container.encode(latitude, forKey: .latitude)
  74. try container.encode(longitude, forKey: .longitude)
  75. }
  76. }