Ios MapView没有';删除MKAnnotation Swift4后更新视图

Ios MapView没有';删除MKAnnotation Swift4后更新视图,ios,swift,mapkit,mkannotation,Ios,Swift,Mapkit,Mkannotation,我有一个MapView,您可以通过Firebase在其中添加或删除MKAnnotation。 当您添加新警报时,它会发布到Firebase,在Firebase上我可以观察添加和删除的快照 Firebase得到正确更新,映射得到正确更新,用于添加的快照,但不用于删除的快照。我在这两个函数中检查保存警报的阵列在接收快照之前和之后是否正确更新,并且它们确实是正确的 因此,唯一没有发生的事情就是从地图上删除图标。。使用基于传入快照定义的self.mapView.removeAnnotation(anno

我有一个
MapView
,您可以通过Firebase在其中添加或删除
MKAnnotation
。 当您添加新警报时,它会发布到Firebase,在Firebase上我可以观察添加和删除的快照

Firebase得到正确更新,映射得到正确更新,用于添加的快照,但不用于删除的快照。我在这两个函数中检查保存警报的阵列在接收快照之前和之后是否正确更新,并且它们确实是正确的

因此,唯一没有发生的事情就是从地图上删除图标。。使用基于传入快照定义的
self.mapView.removeAnnotation(annotationToRemove)
时。 如果我删除所有注释并从数组中重新添加它们,它将正常工作。看到这张不断更新的地图真是太可怕了。。看起来更像是一个小故障,然后更新地图。 你能理解为什么删除特定的一个不起作用吗?? 一如既往,非常感谢你。 代码如下:

func getAlerts(setCompletion: @escaping (Bool) -> ()) {

//        self.mapView.removeAnnotations(mapView.annotations)
//        MapArray.alertNotificationCoordinatesArray.removeAll()
//        MapArray.userAlertNotificationArray.removeAll()

        print("  MapArray.alertNotificationCoordinatesArray before getAlerts is: \(MapArray.alertNotificationCoordinatesArray)")
        print(" MapArray.userAlertNotificationArray before getAlerts is: \(MapArray.userAlertNotificationArray)")

        ref = Database.database().reference()
        //        ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observe(.childAdded, with: { (snapshot) in

        ref?.child("Continent").child("Europe").child("Country").child("\(String(describing: userDetails.country!))").child("Region").child("\(String(describing: userDetails.region!))").child("City").child("\(String(describing: userDetails.city!))").child("Community").child("Alert Notifications").observe(DataEventType.childAdded, with: { (snapshot) in
//            self.mapView.removeAnnotations(self.mapView.annotations) // wrong!! causes all annotations to be deleted when any new one is  notified by anyone
//            print(" added snapshot is: \(snapshot)")
            guard let data = snapshot.value as? [String:String] else { return }

            //            guard let firebaseKey = snapshot.key as? String else { return }
            let firebaseKey = snapshot.key
            let dataLatitude = data["Latitude"]!
            let dataLongitude = data["Longitude"]!
            let type = data["Description"]!
            //            let id = Int(data["Id"]!)
            let id = data["Id"]!
            let userName = data["user"]!
            let alertImageUrl = data["alertImageUrl"] ?? ""
            let alertImageName = data["alertImageName"] ?? ""
            let doubledLatitude = Double(dataLatitude)
            let doubledLongitude = Double(dataLongitude)
            let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)

            let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type, id: id, userName: userName, alertImageUrl: alertImageUrl, alertImageName: alertImageName)
            MapArray.userAlertNotificationArray.append(userAlertAnnotation)  // array of notifications coming from Firebase
            MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate) // array for checkig alerts on route
            print("  MapArray.alertNotificationCoordinatesArray after getNewerAlerts is: \(MapArray.alertNotificationCoordinatesArray)")
            print("   MapArray.userAlertNotificationArray after getNewerAlerts is: \(MapArray.userAlertNotificationArray)")
            self.mapView.addAnnotation(userAlertAnnotation)
            setCompletion(true)
//            self.mapView.addAnnotations(MapArray.userAlertNotificationArray)
        })
    }



func getDeletedAlerts(setCompletion: @escaping (Bool) -> ()) {

        ref?.child("Continent").child("Europe").child("Country").child("\(String(describing: userDetails.country!))").child("Region").child("\(String(describing: userDetails.region!))").child("City").child("\(String(describing: userDetails.city!))").child("Community").child("Alert Notifications").observe(DataEventType.childRemoved, with: { (snapshot) in

            print("    MapArray.userAlertNotificationArray before getDeletedAlerts snapshot is: \(MapArray.userAlertNotificationArray)")
            print("    MapArray.alertNotificationCoordinatesArray before getDeletedAlerts snapshot is: \(MapArray.alertNotificationCoordinatesArray)")

            print("        removed snapshot is: \(snapshot)")
            guard let data = snapshot.value as? [String:String] else { return }
            let firebaseKey = snapshot.key
            let dataLatitude = data["Latitude"]!
            let dataLongitude = data["Longitude"]!

            let type = data["Description"]!
//            let id = Int(data["Id"]!)
            let id = data["Id"]!
            let userName = data["user"]!
            let alertImageUrl = data["alertImageUrl"] ?? ""
            let alertImageName = data["alertImageName"] ?? ""
            let doubledLatitude = Double(dataLatitude)
            let doubledLongitude = Double(dataLongitude)
            let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)


            let annotationToRemove = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type, id: id, userName: userName, alertImageUrl: alertImageUrl, alertImageName: alertImageName)

            MapArray.userAlertNotificationArray.removeAll(where: { ($0.firebaseKey == firebaseKey) }) //remove the alert
            MapArray.alertNotificationCoordinatesArray.removeAll(where: { ($0.latitude == recombinedCoordinate.latitude && $0.longitude == recombinedCoordinate.longitude) })

            self.mapView.removeAnnotation(annotationToRemove)
//            self.mapView.removeAnnotations(self.mapView.annotations)
//            self.mapView.addAnnotations(MapArray.userAlertNotificationArray)

            print("    MapArray.userAlertNotificationArray after getDeletedAlerts snapshot is: \(MapArray.userAlertNotificationArray)")
            print("    MapArray.alertNotificationCoordinatesArray after getDeletedAlerts snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
            setCompletion(true)
        })
    }

您可以创建注释并尝试将其删除,但该注释肯定不会添加到地图视图中

let annotationToRemove = UserAlert( 
self.mapView.removeAnnotation(annotationToRemove)
而你应该这样做

for item in  self.mapView.annoations {
   if let ann = item as? UserAlert , ann.id == annotationToRemove.id {
       self.mapView.removeAnnotation(ann)
   }
}

当然那就是我忘了做的。。在
mapView.annotations
中查找它。感谢您一直在这里帮助像我这样的不熟练开发人员。这给我带来了一个错误:
类型为“MKAnnotation”的值没有成员“id”