0

I've been reading Geofire documentation but for some reason, I cannot save the location. I'm trying to save the locations after login. I debugged it and observed that it doesn't even get into the if error. It also doesn't get latitude and longitude coordinates.

Am I missing something? Thank you

var geoFireRef: DatabaseReference!
var geoRef: GeoFire!

override func viewDidLoad() {
    super.viewDidLoad()
    setUpElements()

    configureLocationManage()
    geoFireRef = Database.database().reference()
    geoRef = GeoFire(firebaseRef: geoFireRef)
}

@IBAction func loginButtonTapped(_ sender: Any) {

    // clean fields

    let email = emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
    let pass = passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)



    Auth.auth().signIn(withEmail: email, password: pass){
        (result,error) in
        if error != nil{
            self.labelError.text = error!.localizedDescription
            self.labelError.alpha=1
        }else {

            //GEOFire
           let userLat = UserDefaults.standard.double(forKey: "current_latitude")
           let userLong  = UserDefaults.standard.double(forKey: "current_longitude")

            let location:CLLocation = CLLocation(latitude: CLLocationDegrees(userLat), longitude: CLLocationDegrees(userLong))

           self.geoRef?.setLocation(location, forKey: Auth.auth().currentUser!.uid){ (error) in
           if (error != nil) {
             print("An error occured: \(error)")
           } else {
             print("Saved location successfully!")
           }

            }




            let homeViewController = self.storyboard?.instantiateViewController(identifier: constants.Storyboard.homeViewController) as? HomeViewController
            self.view.window?.rootViewController = homeViewController
            self.view.window?.makeKeyAndVisible()
        }

    }
}
Frak
  • 832
  • 1
  • 11
  • 32
Gaby
  • 25
  • 1
  • 5
  • Whats this for? `let userLat = UserDefaults.standard.double(forKey: "current_latitude")`? Are you saving something in UserDefaults prior to that call? – Jay Jun 07 '20 at 14:17
  • Thank you for answer back.I'm really new to this so I assumed I was getting default the latitude and longitude. I also tried sending values directly but not luck. – Gaby Jun 07 '20 at 17:00
  • Are you asking how to get the devices lat and long? Or are you just having difficulty writing it once you have it? The [GeoFire Docs](https://github.com/firebase/geofire-objc#swift-2) cover a lot of it. – Jay Jun 08 '20 at 17:04

1 Answers1

0

From GeoFire for iOS:

geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: "firebase-hq") { (error) in
  if (error != nil) {
    print("An error occured: \(error)")
  } else {
    print("Saved location successfully!")
  }
}

Don't forget to replace latitude and forKey with your values. Let me know if this works for you.

sllopis
  • 2,292
  • 1
  • 8
  • 13
  • Well, that's a copy and paste from the documentation for [setLocation](https://github.com/firebase/geofire-objc#swift-1) but I don't think that's what the OP is after. They want to get their latitude and longitude. – Jay Jun 08 '20 at 17:03
  • Hi @Jay, thanks for your comment. If I didn't misread the original question, I believe the OP is having both issues, getting the `latitude` and `longitude` **and** saving the `location`. I believe that my answer, based on the [docs](https://github.com/firebase/geofire-objc#geofire-for-ios-quickstart), can help the OP find out whether it's an issue with the `setLocation` method, or rather with the `latitude` and `longitude` paramaters (since they are hard coded in the example). I'll stand by in case I can help further. – sllopis Jun 09 '20 at 07:28