2

Below I have some data in firestore.enter image description here

I have a an array in the _geoloc field. Each of those indexes have latitude and longitude coordinates. So using SWIFT I want to be able to only get the lat and lng coordinates at index 0 and pass them to individually to a string. I have been researching for days and I am stuck. I have tried to create a new array of strings or an AnyObject array and I just get stuck with retrieving the data I need at index 0, and passing only those 2 lat/lng coordinates to string values. I have several failed snippets of code I could post.

Here is a snippet of what I was attempting to do: (I am really new to firebase so the code is a bit ugly as I am just trying to figure this out)

        Firestore.firestore().collection("users").document("626").getDocument { (document, error) in
        if let document = document {
//            let geo_array = document["_geoloc"]

            var yourArray = [String]()
           // let geo_location = [geo_array] as [AnyObject]
            let array: [Any] =  document["_geoloc"] as! [Any]
            let tmpArray = array.map({ return String(describing: $0)})
            let string = tmpArray.joined(separator: ",")
           yourArray.append(string)

            print(yourArray[0])

2 Answers 2

3

You just need to cast your object from Any to an array of dictionaries and get the first property:

Firestore.firestore().collection("users").document("626").getDocument { document, error in
    if let document = document {
        var yourArray: [String] = []    
        if let location = (document["_geoloc"] as? [[String:Double]])?.first,
            let latitude = location["lat"],
            let longitude = location["lon"] {
            let coordinate2d = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
            yourArray.append("Latitude: \(location.latitude), Longitude: \(location.Longitude)")
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, I see, I was declaring my array wrong. This answers my question and I will mark it, but as a follow up, if I wanted to get the second, third, or even 10th index, what would be the right syntax for that? [[String:Double]])?[2] is not correct.
Just remove the first property you will have an array of dictionaries. You can map them as needed. Right now I am on my mobile but I will edit the post once I get a chance.
I figured it out, I was referencing the index at the wrong spot. if let location = (document["_geoloc"] as? [[String:Double]]), let latitude = location[1]["lat"] { Appreciate the help @leo-dabus, apparently I need to hit the swift books and get better educated on arrays.
0

Maybe first declare this:

  var firstPositionValues = [AnyObject]()

Then get it like this:

let db = Firestore.firestore().collection("users").document("626")

 db.getDocument { (document, error) in
        if let document = document {

            let geo_location = document["_geoloc"] as [AnyObject] // <- this is all of them in the document
            var initialValues = geo_location[0] // <- here are your lat and lng at position 0
             self.firstPositionValues = initialValues

        }

Hope I understood correctly good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.