【问题标题】:GeoFire setLocation method does not save location to Firebase DatabaseGeoFire setLocation 方法不会将位置保存到 Firebase 数据库
【发布时间】:2020-06-07 05:58:45
【问题描述】:

我一直在阅读 Geofire 文档,但由于某种原因,我无法保存该位置。我正在尝试在登录后保存位置。我调试它并观察到它甚至没有进入if error。它也没有纬度和经度 坐标。

我错过了什么吗? 谢谢你

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()
        }

    }
}

【问题讨论】:

  • 这是干什么用的? let userLat = UserDefaults.standard.double(forKey: "current_latitude")?在调用之前,您是否在 UserDefaults 中保存了一些内容?
  • 感谢您的回复。我对此很陌生,所以我认为我得到了默认的纬度和经度。我也尝试过直接发送值,但不是运气。
  • 您在问如何让设备经纬度吗?或者你只是在拥有它后难以编写它? GeoFire Docs 涵盖了很多内容。

标签: swift firebase google-cloud-firestore geofire iosdeployment


【解决方案1】:

来自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!")
  }
}

不要忘记将 latitudeforKey 替换为您的值。让我知道这是否适合您。

【讨论】:

  • 嗯,这是从setLocation 的文档中复制和粘贴的,但我认为这不是 OP 所追求的。他们想得到他们的纬度和经度。
  • 嗨@Jay,感谢您的评论。如果我没有误读原始问题,我相信 OP 有这两个问题,获取 latitudelongitude 保存 location。我相信我的回答,基于docs,可以帮助 OP 找出是 setLocation 方法的问题,还是 latitudelongitude 参数的问题(因为它们在例子)。如果我能提供进一步的帮助,我会待命。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多