Sfoglia il codice sorgente

fix:优化接受地图一些参数的格式。

“HeShaoZe” 4 mesi fa
parent
commit
87a6b1c23e

+ 67 - 16
plugins/map_mapkit_ios/ios/Classes/MapView/Model/Models.swift

@@ -67,14 +67,35 @@ class ATMapPolyline: NSObject, Codable {
     required init(from decoder: Decoder) throws {
         let container = try decoder.container(keyedBy: CodingKeys.self)
         
-        lineId = try container.decode(String.self, forKey: .lineId)
-        lineType = try container.decode(String.self, forKey: .lineType)
-        points = try container.decode([CLLocationCoordinate2D].self, forKey: .points)
-        color = try container.decode(String.self, forKey:.color)
-        width = try container.decode(Double.self, forKey: .width)
-        
+        if container.contains(.lineId) {
+            lineId = try container.decodeIfPresent(String.self, forKey: .lineId) ?? ""
+        } else {
+            lineId = ""
+        }
+        if container.contains(.lineType) {
+            lineType = try container.decodeIfPresent(String.self, forKey: .lineType) ?? ""
+        } else {
+            lineType = "";
+        }
+        if container.contains(.points) {
+            points = try container.decodeIfPresent([CLLocationCoordinate2D].self, forKey: .points) ?? [CLLocationCoordinate2D]()
+        } else {
+            points = [CLLocationCoordinate2D]()
+        }
+        if container.contains(.color) {
+            color = try container.decodeIfPresent(String.self, forKey: .color) ?? ""
+        } else {
+            color = ""
+        }
+        if container.contains(.width) {
+            width = try container.decodeIfPresent(Double.self, forKey: .width) ?? 0
+        } else {
+            width = 0
+        }
         if container.contains(.mapPadding) {
-            mapPadding = try container.decodeIfPresent(ATMapPadding.self, forKey: .mapPadding)
+            mapPadding = try container.decodeIfPresent(ATMapPadding.self, forKey: .mapPadding) ?? ATMapPadding(top: 0, left: 0, bottom: 0, right: 0)
+        } else {
+            mapPadding = ATMapPadding(top: 0, left: 0, bottom: 0, right: 0)
         }
     }
     
@@ -101,6 +122,7 @@ class ATMapPolyline: NSObject, Codable {
 
 class ATMapMarker: NSObject, Codable {
     var id: String
+    var markerId: String
     var markerName: String?
     var customAvatarUrl : String?
     var latitude: CGFloat
@@ -109,8 +131,9 @@ class ATMapMarker: NSObject, Codable {
     var markerType: any MapMarkerSupportType
     var tags: [String : String]?
 
-    init(id: String, markerName: String?, customAvatarUrl: String?,location: CLLocationCoordinate2D, markerType: any MapMarkerSupportType, isSelected: Bool = false,tags: [String : String]?) {
+    init(id: String, markerId: String,markerName: String?, customAvatarUrl: String?,location: CLLocationCoordinate2D, markerType: any MapMarkerSupportType, isSelected: Bool = false,tags: [String : String]?) {
         self.id = id
+        self.markerId = markerId
         self.markerName = markerName
         self.customAvatarUrl = customAvatarUrl
         self.latitude = location.latitude
@@ -128,7 +151,7 @@ class ATMapMarker: NSObject, Codable {
     }
 
     enum CodingKeys: String, CodingKey {
-        case id, markerName,customAvatarUrl, latitude, longitude, isSelected, markerType,tags
+        case id,markerId,markerName,customAvatarUrl, latitude, longitude, isSelected, markerType,tags
     }
 
     required init(from decoder: Decoder) throws {
@@ -138,18 +161,40 @@ class ATMapMarker: NSObject, Codable {
             id = "user_location"
         }
         if container.contains(.markerName) {
-            markerName = try container.decodeIfPresent(String.self, forKey: .markerName)
+            markerName = try container.decodeIfPresent(String.self, forKey: .markerName) ?? ""
+        } else {
+            markerName = ""
         }
         if container.contains(.customAvatarUrl) {
-            customAvatarUrl = try container.decodeIfPresent(String.self, forKey: .customAvatarUrl)
+            customAvatarUrl = try container.decodeIfPresent(String.self, forKey: .customAvatarUrl) ?? ""
+        } else {
+            customAvatarUrl = ""
         }
-        latitude = try container.decode(CGFloat.self, forKey: .latitude)
-        longitude = try container.decode(CGFloat.self, forKey: .longitude)
-        isSelected = try container.decode(Bool.self, forKey: .isSelected)
+        
+        if container.contains(.latitude) {
+            latitude = try container.decodeIfPresent(CGFloat.self, forKey: .latitude) ?? 0
+        } else {
+            latitude = 0
+        }
+        if container.contains(.longitude) {
+            longitude = try container.decodeIfPresent(CGFloat.self, forKey: .longitude) ?? 0
+        } else {
+            longitude = 0
+        }
+        if container.contains(.isSelected) {
+            isSelected = try container.decodeIfPresent(Bool.self, forKey: .isSelected) ?? false
+        } else {
+            isSelected = false
+        }
+   
         
         // 使用工厂方法创建正确类型的markerType
-        let rawValue = try container.decode(Int.self, forKey: .markerType)
-        markerType = MarkerTypeFactory.markerType(from: rawValue)
+        if container.contains(.markerType) {
+            let rawValue = try container.decodeIfPresent(Int.self, forKey: .markerType) ?? 1
+            markerType = MarkerTypeFactory.markerType(from: rawValue)
+        } else {
+            markerType = MarkerTypeFactory.markerType(from: 1)
+        }
         
         ///气泡内容
         // 处理tags为空的情况
@@ -158,6 +203,11 @@ class ATMapMarker: NSObject, Codable {
          } else {
              tags = [:] // 默认为空字典
          }
+        if container.contains(.markerId) {
+            markerId = try container.decodeIfPresent(String.self, forKey: .markerId) ?? ""
+        } else {
+            markerId = ""
+        }
     }
 
     func encode(to encoder: Encoder) throws {
@@ -170,6 +220,7 @@ class ATMapMarker: NSObject, Codable {
         try container.encode(isSelected, forKey: .isSelected)
         try container.encode(markerType.rawValue, forKey: .markerType)
         try container.encode(tags, forKey: .tags)
+        try container.encode(markerId, forKey: .markerId)
     }
 }
 

+ 1 - 1
plugins/map_mapkit_ios/ios/Classes/MapView/ViewModel/MapViewModel.swift

@@ -126,7 +126,7 @@ class MapViewModel: NSObject, ObservableObject, MapCapability {
         // 使用标识符找到要移除的标记物并从数组中删除
         
         DispatchQueue.main.async{
-            if let index = self.markers.firstIndex(where: { $0.id == markerItem.id }) {
+            if let index = self.markers.firstIndex(where: { $0.id == markerItem.markerId }) {
                 self.markers.remove(at: index)
             }
         }