2017-04-27 33 views
1

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

这是我如何检索火力和显示标记数据在为cicle

​​

我也把我用它来创建标记

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

注:该功能我可是从火力地堡

编辑手动删除数据: This is a gif for a better explain

+0

哪里是你的删除标记代码? – CodeChanger

+0

即时只能从数据库手动删除 – GaTz

+0

您是否正在从Firebase手动删除? – CodeChanger

回答

0

按我的先进经验,你再次呼唤火力数据或refresing它阿恩

如果是比你有下面清除地图方法

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

+0

感谢您的回答,但如果我从数据库删除数据标记停留在它的位置,所以是相同的 – GaTz

+0

你打电话给[mapView清除]方法? – CodeChanger

+0

是的,有人说我有关“欺骗”:在字典中添加标记并将数值与数据库进行比较。在java人们使用hashmap,但我不知道如何迅速 – GaTz