Martin Utzon
Martin Utzon

Reputation: 87

retrieve array elements from firebase in swift

I am completely new to swift and firebase, and I am having difficulties in retrieving array-elements from firebase database

So this is my firebase database Firebase Database

I can retrieve the other elements like this:

database reference

class ViewController: UIViewController {

var ref: FIRDatabaseReference?
let fileName : String = "jsonFile"

method

    func parseFirebaseResponse() { 
    ref?.child("Vandreture").child(fileName).observe(.value, with: 
    { (snapshot) in
            let dict = snapshot.value as? [String: AnyObject]
            let navn = dict!["navn"] as? String
            print(navn as Any)

            let type = dict!["type"] as? String
            print(type as Any)

            let Længde = dict!["length"] as? String
            print(Længde as Any)

            let link = dict!["link"] as? String
            print(link as Any)

        })

}

the console shows the result enter image description here

But I have searched for a way to retrieve longitude/latitude pairs, The first pair should be latitude 109.987 longitude 102.987 - but so far without luck - help would really be appreciated :)

Upvotes: 0

Views: 1555

Answers (3)

Martin Utzon
Martin Utzon

Reputation: 87

thanks Victor Apeland - now I changed the structure of the database. I was not able to do it exactly as you suggested!! I concatenated the lon/lat as a string (not the best - i know - I'm a newbie to swift, and firebase is not well documented) enter image description here to retrieve the individual elements from my long/lat, I used this method, and I was particular interested in the first reading, so I made a list, populated it, and got the first element, parsing it to a new method

func responsePosition() {
    var positions = [String]()
    ref?.child("Vandreture").child(fileName).child("position").observe(.value, with: { (snapshot) in

    for snap in snapshot.children {
        let userSnap = snap as! FIRDataSnapshot
        let uid = userSnap.key //the uid of each user
        let userDict = userSnap.value as! String
        positions.append(userDict)

    }
      let  pos : String = positions[0]
        self.formatPositions(pos : pos)
    })
}
  • In my firebase lon-lat was divided by a space, so I split the string and cast to double.

    func formatPositions(pos : String) {
    let lotLangDivided = pos.components(separatedBy: " ")
    let longitude = Double(lotLangDivided[0])
    let latitude = Double(lotLangDivided[1]) 
    }
    

now I got the result i wanted - thanks for the help Victor and Khuong :)

Upvotes: 0

Twitter khuong291
Twitter khuong291

Reputation: 11672

Get your "position" as a dictionary (aka [String: AnyObject]).

Then get array of latitude and longitude. Then map of each element from latitude and longitude into a tuple or something like that.

guard let position = dict!["position"] as? [String: AnyObject], 
let latitudeArray =  position["latitude"] as? [String], 
let longitudeArray =  position["longitude"] as? [String] else { return }
print(latitudeArray)
print(longitudeArray)

Upvotes: 0

Victor Apeland
Victor Apeland

Reputation: 110

I think you should restructure your database to something like this:

Vandreture {
   jsonFile {
       length: "16.2"
       link: "www.second.com"
       navn: "Kagerup rundt"
       positions {
           -KqXukxnw3mL38oPeI4y {
               x: "102.987"
               y: "109.987"
           }
           -KqXukxnw3mL38oPeI5- {
               x: "108.234"
               y: "99.098"
           }
       }
   }
}

Then getting your coordinates would be far easier!

Upvotes: 1

Related Questions