Google 从 firebase 检索到的地图标记
Google Map markers retrieved from firebase
创建标记后,它们会立即显示在地图和数据库中。
但是当我删除它时,它会保留在地图中,直到我更新视图。我想立即将其从地图中删除,但不知道如何操作。
有什么建议吗?
这就是我从 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)
}
N.B。我从 Firebase
手动删除数据
编辑:
This is a gif for a better explain
根据我的经验,您正在再次调用 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 {
//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() 方法。
查看下方 link 参考:
https://developers.google.com/maps/documentation/ios-sdk/marker
创建标记后,它们会立即显示在地图和数据库中。 但是当我删除它时,它会保留在地图中,直到我更新视图。我想立即将其从地图中删除,但不知道如何操作。 有什么建议吗?
这就是我从 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)
}
N.B。我从 Firebase
手动删除数据编辑: This is a gif for a better explain
根据我的经验,您正在再次调用 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 {
//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() 方法。
查看下方 link 参考:
https://developers.google.com/maps/documentation/ios-sdk/marker