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

时间:2019-04-03 04:40:13

标签: swift firebase google-maps-markers infowindow gmsmapview

因此,我试图将已从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
}

我不确定Im所做的是否正确。就像我说的,我只希望将每个标记的快照数据显示到该特定标记。

1 个答案:

答案 0 :(得分:0)

所以我设法解决了这个问题。

我添加了

 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
}

效果很好!