所以我试图将我从firebase快照的数据传输到自定义信息窗口。我目前有四种不同的活动类别,因此有四种不同的标记数组和功能。我有一个通用的自定义信息窗口,我希望显示标记标题,评级和难度级别。
我有一个问题我目前尝试将所有数据附加到一个结构化数组中,然后从该数组中调用数据,但我获得的是所有信息窗口中的相同数据集。我希望每个信息窗口专门显示与该GMS标记相关的数据。
这是显示董事会活动的功能。我为每个活动提供了四个这样的功能。
func showBoardIcon() {
ref = Database.database().reference()
ref.child("location").observe(.childAdded) { (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject] {
if dict["Activity"] as! String == "Board" {
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
let key = dict["id"] as! String
self.boardIconArray.insert(coordinate(title: title, carLat: lattitude, carLng: longitude, idKey: key), at: 0)
let n = self.boardIconArray.count
let heightWidth = (self.mapView.frame.height / 12)
for var Marker in 1...n {
let boardMarker = GMSMarker()
let boardIconView = UIImage(named: "boardPin")
let image = boardIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
boardMarker.position = location
boardMarker.icon = image
boardMarker.title = title
boardMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth, height: heightWidth))
func displayBoard() {
if self.boardNumber == "1" {
boardMarker.map = self.mapView
self.arrBoardMarker.append(boardMarker)
} else {
boardMarker.map = nil
}
}
displayBoard()
break
}
}
}
}
}
这是显示自定义信息窗口的功能。
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let Markerview: infoWindow = UIView.fromNib()
let a = arrCarMarkers.count
let b = arrLegMarker.count
let c = arrWaterMarker.count
let d = arrBoardMarker.count
let all = 0 + a + b + d + c
Markerview.titleLbl.text = arrAllMarkers[key].title
Markerview.ratingLbl.text = ("\(arrAllMarkers[all].rating)/5")
Markerview.difficultyLbl.text = arrAllMarkers[all].diff
Markerview.idKey.text = arrAllMarkers[all].key
transferKey = arrAllMarkers[all].key
Markerview.alpha = 0.8
Markerview.layer.cornerRadius = 30
return Markerview
}
我不确定我做的事情是否正确。就像我说的那样,我只想让每个标记的快照数据显示给该特定标记。
所以我管理来解决这个问题。
我补充道
boardMarker.title = key
在创建标记的循环内部。
然后我写了这部分代码
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let markerTitle = marker.title as! String
let Markerview: infoWindow = UIView.fromNib()
filteredStruct = arrAllMarkers.filter{$0.key.range(of: markerTitle) != nil}
print(filteredStruct)
Markerview.titleLbl.text = filteredStruct[0].title
Markerview.ratingLbl.text = filteredStruct[0].rating
Markerview.difficultyLbl.text = filteredStruct[0].diff
transferKey = markerTitle
Markerview.alpha = 0.8
Markerview.layer.cornerRadius = 30
print(transferKey)
return Markerview
}
它完美无缺!