0

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 :)

1

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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)

2 Comments

When I add your code to my method, run the program and check the debugger, the "position" has 2 key/value pairs which are "longitude" and "latitude", but latitudeArray and longitudeArray do not contain any elements so they don't print anything to the console
sorry, this should be let longitudeArray = position["longitude"] instead of let longitudeArray = position["latitude"] :D
0

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 :)

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.