Search code examples
swiftfirebasegoogle-maps-markersinfowindowgmsmapview

Transferring Data from Firebase into Custom Info Window


So I'm trying to transfer the data I've snapshotted from firebase into the custom info window. I currently have four different categories of activities and as such there are four different marker arrays and functions. I have one generic custom info window that I wish to display the markers Title, rating and difficulty level into.

I am having an issue Ive currently tried appending all data into one structured array and then calling the data from that array but all I get is the same set of data in all the info windows. I want each info window to specifically display the data associated with that GMS Marker.

This is is the function that shows the board activities. I have four of these functions for each 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
                }
            }
        }
    }
}

This is the function that displays the custom info 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
}

Im not sure if what Im doing is even correct. Like I said I just want the data being snapshotted for each marker to be shown to that specific marker.


Solution

  • So I Managed to solve the issue.

    I added

     boardMarker.title = key
    

    inside the loop where the Marker is being created.

    I then wrote this section of code

    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
    }
    

    and it works perfectly!