Ctibor Šebák
Ctibor Šebák

Reputation: 133

Swift - Retrieving Geopoints from Firestore, how to show them as map annotations

I have the following code which retrieves coordinates I have stored in Google Firestore:

    let locationsRef = db.collection("locatioInfo")
    locationsRef.getDocuments { (querySnapshot, err) in
        if let err = err {
            print("Error receiving Firestore snapshot: \(err)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
            }
        }
    }

which produces this output:

location1 => ["geopoint": <FIRGeoPoint: (50.086421, 14.452333)>]
location2 => ["geopoint": <FIRGeoPoint: (50.086442, 14.452268)>]

My question is: how do I transform those fetched FIRGeoPoints into CLLocationCoordinates2D or simply: how can I use these coordinates and show them on the map?

I have a MKAnnotationView already set-up ad used it for hard-coded coordinates, now I need to display the fetched ones. Hopefully this is clear enough, thanks in advance.

Upvotes: 2

Views: 4363

Answers (1)

Jay
Jay

Reputation: 35667

I think what you are asking is how to derive the latitude and longitude from the Firestore geopoint (??). Try this and if it's not what you are asking, let me know and I will edit.

assume for a moment you have a Firestore structure like this

coordinates
   point_0
       coords: [49N, 44E]
   point_1
       coords: [67N, 30E]

then to read those in and get the lat and lon.

self.db.collection("coordinates").getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in querySnapshot!.documents {
            if let coords = document.get("coords") {
                let point = coords as! GeoPoint
                let lat = point.latitude
                let lon = point.longitude
                print(lat, lon) //here you can let coor = CLLocation(latitude: longitude:)
            }
        }
    }
}

and the output is

49.0 44.0
67.0 30.0

Upvotes: 6

Related Questions