我正在使用密钥“status”从firebase检索数据。如果状态为true,我将显示标记。如果为false,则标记需要从地图中消失。我花了无数个小时做这个应该工作,但我不知道我错过了什么。提前感谢您的帮助。
func ihelprLocationCoordinateMappers() {
dbFir.child("frontEnd/users").observeEventType(.Value, withBlock: { (snapshot) in
let receivePostSnapshot = snapshot.childSnapshotForPath("receivePost")
for child in snapshot.children {
let receivePostSnapshot = child.childSnapshotForPath("receivePost")
let coordinates = receivePostSnapshot.childSnapshotForPath("userCoord").value as! [String: AnyObject]
let lat = coordinates["lat"] as! Double
let long = coordinates["long"] as! Double
let position = CLLocationCoordinate2DMake(lat, long)
let marker = GMSMarker(position: position)
let status = receivePostSnapshot.value!["status"] as! Bool
if status {
// marker will be placed on the map
marker.map = self.mapView
} else {
// if status equal false marker need to dissapear
print("status will not show")
// for somereason the maker is is not dissapearing
if marker != nil {
marker.map = nil
}
}
}
})
}
答案 0 :(得分:0)
首先检查两件事: -
1。)您是否在mapView
中设置了viewDidLoad
的委托,如果未在viewDidLoad函数中设置mapView.delegate = self
2.。)尝试打印status
和position
,并检查收到的位置和状态是否与您的数据库匹配。
如果以上两者都没问题,那么试试这个: -
let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
if let status = receivePostSnapshot.Value!["status"] as! Bool{
if status == true {
let marker = GMSMarker(position: location)
marker.map = self.mapView
} else if (status == false && marker != nil){
marker.map = nil
}
}else{
if marker != nil {
marker.map = nil
}
}