Search code examples
swiftfirebasefirebase-realtime-databasemapkitmapkitannotation

Loading map pins from firebase onto mapkit in swift for all users to be able to see


Im creating a tagging app, where a user can tag street art to a map, this information is then available for all users to see.

I can upload my data to Firebase however Im struggling being able to pull this data back down to appear on the map as an annotation.

Firebase Data Structure

I have created a loadCustomAnnotations() to load up the database information as a pin on my map, I know I need to observe the values and just load in the "art" path, and loop over all the results however I cant seem to turn this into an annotation! can anyone help?! thanks.

func loadCustomLocations() {

    FIRDatabase.database().reference(withPath: "art")

    let artPin = FIRDatabase.database().reference(withPath: "art")

    artPin.observe(.value, with: { snapshot in


        for item in snapshot.children {
            guard let snapshot = item as? FIRDataSnapshot else { continue }

            let ref = art(snapshot: snapshot)

            print(snapshot)

            self.artDrops.append(ref)

     //       self.makeAnnotation(artDrops: art)


        }
    })
}

}

this function now prints all my locations i just need to know how to turn this into an annotation!

Printing Locations


Solution

  • As soon as you are now able to upload and get back all you locations, the following steps won't be so hard. Just using .addAnnotations and you are good to go!

    let artAnnotation = MKPointAnnotation()
                var artDrops = [CLLocation]()
                for location in artDrops{
                artAnnotation.coordinate = location.coordinate
                artAnnotation.title = "your location title"
                mapView.addAnnotations([artAnnotation])
                }