我一直在阅读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()
}
}
}
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!")
}
}
不要忘记用您的值替换latitude
和forKey
。让我知道这是否适合您。