创建标记后,它们会立即显示在 map 和数据库中。 但是当我删除它时,它会保留在 map 中,直到我更新 View 。我想立即将其从 map 中删除,但不知道如何操作。 有什么建议吗?
这就是我从 firebase 检索数据并在 for cicle 中显示标记的方式
let ref = FIRDatabase.database().reference().child("userdata")
ref.observe(.childAdded, with: { (snapshot) in
ref.observe(FIRDataEventType.value, with: { (snapshot) in
if (snapshot.value as? [String:Any]) != nil {
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let Dict = rest.value as? [String: AnyObject] else {
continue
}
let latitude = Dict["latitude"]
let longitude = Dict["longitude"]
let username = Dict["username"]
let model = Dict["firstname"]
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees, longitude: longitude as! CLLocationDegrees)
marker.title = username as? String
marker.snippet = model as? String
mapView.selectedMarker = marker
marker.map = mapView
}
}
})
})
我还放置了我用来创建标记的函数
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
let alertController = UIAlertController(title: "Crea Marker", message: "...", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Annulla", style: UIAlertActionStyle.cancel) {
(result : UIAlertAction) -> Void in
print("Annulla")
}
// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "Crea", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
let ref = FIRDatabase.database().reference().child("userdata")
let currentUser = FIRAuth.auth()?.currentUser
let displayName = currentUser?.displayName
let post = ["username": displayName,"uid": currentUser?.uid as Any,"latitude": coordinate.latitude,"longitude": coordinate.longitude, "firstname":"test"]
ref.childByAutoId().setValue(post)
print("Entra")
}
alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
注意我从 Firebase 手动删除数据
最佳答案
根据我的经验,您正在再次调用 firebase 数据或重新刷新它
如果是,则必须使用以下方法清除 map
let ref = FIRDatabase.database().reference().child("userdata")
ref.observe(.childAdded, with: { (snapshot) in
ref.observe(FIRDataEventType.value, with: { (snapshot) in
if (snapshot.value as? [String:Any]) != nil {
//Here Before adding any other marker You have to clear your MAP
mapView.clear()
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let Dict = rest.value as? [String: AnyObject] else {
continue
}
let latitude = Dict["latitude"]
let longitude = Dict["longitude"]
let username = Dict["username"]
let model = Dict["firstname"]
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees, longitude: longitude as! CLLocationDegrees)
marker.title = username as? String
marker.snippet = model as? String
mapView.selectedMarker = marker
marker.map = mapView
}
}
})
})
它将解决您在 MAP 上使用旧标记的问题
但是,如果您不刷新数据,请在要刷新此数据或删除标记的地方使用此 mapView.clear() 方法。
检查下面的引用链接:
https://developers.google.com/maps/documentation/ios-sdk/marker
关于ios - 从 firebase 检索到的谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43652671/