2017-04-27 43 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