将数据从 Firebase 传输到自定义信息 Window

Transferring Data from Firebase into Custom Info Window

所以我正在尝试将我从 firebase 快照的数据传输到自定义信息中 window。我目前有四种不同类别的活动,因此有四种不同的标记数组和函数。我有一个通用自定义信息 window,我希望在其中显示标记标题、评级和难度级别。

我有一个问题我目前尝试将所有数据附加到一个结构化数组中,然后从该数组中调用数据,但我得到的只是所有信息中的同一组数据 windows。我希望每个信息 window 专门显示与该 GMS 标记关联的数据。

这是显示棋盘活动的函数。每个 activity.

我有四个这样的函数
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
                }
            }
        }
    }
}

这是显示自定义信息的函数window。

 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{[=11=].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
}

而且效果很好!