Ios 从firebase检索到Google地图标记

Ios 从firebase检索到Google地图标记,ios,swift,google-maps,google-maps-markers,Ios,Swift,Google Maps,Google Maps Markers,创建标记后,它们会立即显示在地图和数据库中。 但当我删除它时,它将保留在地图中,直到我更新视图。我想立即在地图上删除它,但不知道如何删除。 有什么建议吗 这就是我如何从firebase检索数据并在cicle的中显示标记的方式 let ref = FIRDatabase.database().reference().child("userdata") ref.observe(.childAdded, with: { (snapshot) in ref.observe(FIRDataEven

创建标记后,它们会立即显示在地图和数据库中。 但当我删除它时,它将保留在地图中,直到我更新视图。我想立即在地图上删除它,但不知道如何删除。 有什么建议吗

这就是我如何从firebase检索数据并在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)
    }
N.B.我正在从Firebase手动删除数据

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
            }
        }
    })
})
编辑:

根据我的经验,您正在再次调用firebase data或重新定义它

如果是,则必须使用以下方法清除地图

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
            }
        }
    })
})
这将解决你们在地图上使用旧标记的问题

但是,如果不刷新数据,请使用此mapView.clear()方法刷新此数据或删除标记

检查以下链接以获取参考:


根据我的经验,您正在再次调用firebase data或重新定义它

如果是,则必须使用以下方法清除地图

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
            }
        }
    })
})
这将解决你们在地图上使用旧标记的问题

但是,如果不刷新数据,请使用此mapView.clear()方法刷新此数据或删除标记

检查以下链接以获取参考:


删除标记的代码在哪里?我只是从数据库手动删除?你是从Firebase手动删除吗?是的,仅用于测试你的删除标记代码在哪里?我只是从数据库手动删除,你是从Firebase手动删除吗?是的,仅用于测试谢谢回答,但是,如果我从数据库中删除数据,那么标记将保持在它的位置,sameYes也一样,有人会告诉我一个“欺骗”:在字典中添加标记,并将值与数据库进行比较。在java中,人们使用hashmap,但我不知道如何在Swift中尝试这个
//在添加任何其他标记之前,您必须清除您的地图
mapView.clear()
您可以共享该屏幕可能是您当前的位置标记吗?谢谢您的回答,但如果我从db中删除数据,标记将保持在其位置,sameYes也是,有人跟我说“作弊”:在字典中添加标记,并将值与数据库进行比较。在java中,人们使用hashmap,但我不知道如何在Swift中尝试此
//在添加任何其他标记之前,您必须清除地图
mapView.clear()
您是否可以共享该屏幕可能是您当前的位置标记?