简体   繁体   English

删除 MKAnnotation Swift4 后 MapView 不更新视图

[英]MapView doesn't update view after removing MKAnnotation Swift4

I have a MapView where you can add or delete MKAnnotation via Firebase.我有一个MapView ,您可以在其中通过 Firebase 添加或删除MKAnnotation When you add a new alert it get posted to Firebase on which I have observers both for added and removed snapshots.当您添加一个新警报时,它会被发布到 Firebase 上,我有观察者来查看添加和删除的快照。

Firebase gets updated correctly, map gets updated correctly for added snapshots, but doesn't get updated for deleted ones. Firebase 已正确更新,map 已正确更新添加的快照,但不会更新已删除的快照。 I check in both functions that the arrays where I save the alerts are correctly updated before and after receiving the snapshots and they indeed are correct.我检查了两个函数,我保存警报的 arrays 在接收快照之前和之后正确更新,它们确实是正确的。

So the only thing not happening is the icon removed from the map.. when I use self.mapView.removeAnnotation(annotationToRemove) which I define based on the incoming snapshot.所以唯一没有发生的是从 map 中删除的图标。当我使用我根据传入快照定义的self.mapView.removeAnnotation(annotationToRemove)时。 If I instead remove all the annotations and re add them from the array it works correctly.It's just horrible to see this continuously updating map.. seems more like a glitching error then an updating map.如果我改为删除所有注释并从数组中重新添加它们,它可以正常工作。看到这个不断更新 map 真是太可怕了。看起来更像是一个故障错误,然后是更新 map。 Can you see why removing the specific one doesn't work??你能明白为什么删除特定的不起作用吗? As always thank you very much.一如既往地非常感谢你。 This is the code:这是代码:

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)
        })
    }

You create the annotation and try to remove it which for sure not added to the mapView您创建注释并尝试删除它肯定没有添加到 mapView

let annotationToRemove = UserAlert( 
self.mapView.removeAnnotation(annotationToRemove)

While you should do虽然你应该做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM