我有一个MapView
,您可以在其中通过Firebase添加或删除MKAnnotation
。
当您添加新警报时,它会发布到Firebase上,在那里我可以看到添加和删除快照的观察者。
Firebase正确更新,对于添加的快照,map正确更新,但对于已删除的快照,map更新不正确。我在这两个函数中都进行了检查,以确保在收到快照之前和之后,用于保存警报的数组已正确更新,并且确实正确。
因此,唯一没有发生的事就是从地图上删除了图标。.当我使用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)
})
}
答案 0 :(得分:1)
您创建注释,并尝试将其删除,但请确保未将其添加到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)
}
}