从firebase检索的Google Map标记

时间:2017-04-27 08:34:44

标签: ios swift google-maps google-maps-markers

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

这是我从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手动删除数据

修改 This is a gif for a better explain

1 个答案:

答案 0 :(得分:0)

根据我的经验,您再次调用firebase数据或者将其重新调整为agin

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

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