ios - 删除 MKAnnotation Swift4 后 MapView 不更新 View

标签 ios swift mapkit mkannotation

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

Firebase 会正确更新, map 会针对添加的快照正确更新,但不会针对已删除的快照进行更新。我检查了这两个函数,我保存警报的数组在接收快照之前和之后正确更新,它们确实是正确的。

所以唯一没有发生的是从 map 中删除的图标..当我使用 self.mapView.removeAnnotation(annotationToRemove)我根据传入的快照定义。
如果我改为删除所有注释并从数组中重新添加它们,它可以正常工作。看到这个不断更新的 map 真是太可怕了.. 看起来更像是一个故障错误,而不是一个更新的 map 。
你能明白为什么删除特定的不起作用吗?
一如既往地非常感谢你。
这是代码:

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

最佳答案

您创建注释并尝试删除它肯定没有添加到 mapView

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

关于ios - 删除 MKAnnotation Swift4 后 MapView 不更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58380001/

相关文章:

Swift - 如何显示一小时的一部分?

objective-c - 确定两个坐标之间的中点

iphone - MapKit 未删除所有注释

ios - 如何设置在 UITextField 中添加的图标的位置?

ios - Firestore 读取旧数据

iphone - 如何遍历多个 UITextField

ios - 打印出连接的字符串显示(空)

ios - 如何设置渐变颜色以在深色模式下查看 ios?

swift - flutter 将 Objective C 项目迁移到 Swift 项目。使用swift插件时出错

ios - iOS6 中的 MapView 在北纬 > 75 时不会显示某些缩放级别