Firebase realtime database 在Xcode 10中使用GeoFire设置用户位置
我的编码(如您将看到的)以及对Xcode和Swift的熟悉是基本的,但我正在学习。。。我意识到我的代码嵌套不是最好的,如果有的话。我正在尝试将GeoFire集成到我的Xcode 10应用程序中。我已经寻找了一个明确的解释,但我仍然没有找到答案。我有一个注册表格来注册用户。最初的Firebase设置非常简单,我有一个功能齐全的表单,可以在Firebase中注册用户。现在我尝试添加GeoFire以跟踪用户的位置 我从YouTube视频和网站上尝试了很多方法,但都不管用Firebase realtime database 在Xcode 10中使用GeoFire设置用户位置,firebase-realtime-database,ios12,swift4.2,geofire,xcode10.1,Firebase Realtime Database,Ios12,Swift4.2,Geofire,Xcode10.1,我的编码(如您将看到的)以及对Xcode和Swift的熟悉是基本的,但我正在学习。。。我意识到我的代码嵌套不是最好的,如果有的话。我正在尝试将GeoFire集成到我的Xcode 10应用程序中。我已经寻找了一个明确的解释,但我仍然没有找到答案。我有一个注册表格来注册用户。最初的Firebase设置非常简单,我有一个功能齐全的表单,可以在Firebase中注册用户。现在我尝试添加GeoFire以跟踪用户的位置 我从YouTube视频和网站上尝试了很多方法,但都不管用 import UIKit im
import UIKit
import Foundation
import CoreLocation
import Firebase
import GeoFire
class SecondViewController: UIViewController {
//Firebase DB ref
var refDriver: DatabaseReference!
@IBOutlet weak var name: UITextField!
@IBOutlet weak var employeenumber: UITextField!
@IBOutlet weak var label: UILabel!
@IBAction func submit(_ sender: UIButton) {
addDriver()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//configuring firebase and ensures no error is returned
if(FirebaseApp.app() == nil){
FirebaseApp.configure()
}
//getting a reference to the node driver
refDriver = Database.database().reference().child("driver");
}
func addDriver(){
//generating a new key inside driver node
//and also getting the generated key
let key = refDriver.childByAutoId().key
//creating driver with the given values
let driver = ["id":key,
"name": name.text! as String,
"employeenumber": employeenumber.text! as String
]
//adding the driver to Firebase
refDriver.child(key!).setValue(driver)
//displaying submit message
label.text = "You are registered."
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
嘿,在安装pod后尝试导入模块
import GeoFire
在viewdidLoad()
函数中调用setupGeofire()
在setupGeoFire中,我创建了一个var geoFireRef,这正是您要保存用户位置信息的节点
let geoFireRef = Database.database().reference().child("user_locations")
然后我们通过传入该节点位置,即我们的geofiref变量,来创建geoFire对象
然后使用
```self.geoFire=geoFire```
如果您想在特定半径(km)内获得用户,也可以检查下面的func
func loadUsersWithin(radius: Double){
guard let uid = Auth.auth().currentUser?.uid else{return}
geoFire?.getLocationForKey(uid, withCallback: { (userLocation, err) in
guard let userLocation = userLocation else{return}
self.myQuery = self.geoFire?.query(at: userLocation, withRadius: radius)
self.myQuery?.observe(.keyEntered, with: { (key, location) in
if key == uid{
return
}
Database.database().reference().child("users").child(key).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String:Any] else{return}
let user = User(uid: key, dictionary: userDictionary)
self.user = user
self.usersArray.append(user)
self.nearbyCV.reloadData()
}, withCancel: { (err) in
print(err)
})
})
})
}
}
这是我的User.swift数据模型
import UIKit
class User{
var name: String
var userImgUrlString: String
var age: String
var uid: String
var aboutMeText:String
init(uid: String,dictionary: [String: Any]) {
self.name = dictionary["username"] as? String ?? ""
self.userImgUrlString = dictionary["profileImageUrlString"] as? String ?? ""
self.aboutMeText = dictionary["aboutMe"] as? String ?? ""
self.uid = uid
self.age = dictionary["age"] as? String ?? ""
}
}```
hope this helps yo it was hard to find info for me too when i just started but it gets better
是的,它确实有帮助,而且比我能找到的要好得多。但是,我现在有两个错误:let user=user(uid:key,(传递给不带参数的调用的参数)和self.nearbyCV.reloadData()(类型为'SecondViewController'的值没有成员'nearbyCV')@Peter User是我用来保存数据的数据模型或结构,因此如果您没有User.swift文件,它将不知道它是什么。我正在使用的UiCollectionView的neabyCV出口。“.reloadData()”调用numberOfRows函数和其他委托方法,以用新数据重新填充collectionView
func loadUsersWithin(radius: Double){
guard let uid = Auth.auth().currentUser?.uid else{return}
geoFire?.getLocationForKey(uid, withCallback: { (userLocation, err) in
guard let userLocation = userLocation else{return}
self.myQuery = self.geoFire?.query(at: userLocation, withRadius: radius)
self.myQuery?.observe(.keyEntered, with: { (key, location) in
if key == uid{
return
}
Database.database().reference().child("users").child(key).observeSingleEvent(of: .value, with: { (snapshot) in
guard let userDictionary = snapshot.value as? [String:Any] else{return}
let user = User(uid: key, dictionary: userDictionary)
self.user = user
self.usersArray.append(user)
self.nearbyCV.reloadData()
}, withCancel: { (err) in
print(err)
})
})
})
}
}
import UIKit
class User{
var name: String
var userImgUrlString: String
var age: String
var uid: String
var aboutMeText:String
init(uid: String,dictionary: [String: Any]) {
self.name = dictionary["username"] as? String ?? ""
self.userImgUrlString = dictionary["profileImageUrlString"] as? String ?? ""
self.aboutMeText = dictionary["aboutMe"] as? String ?? ""
self.uid = uid
self.age = dictionary["age"] as? String ?? ""
}
}```
hope this helps yo it was hard to find info for me too when i just started but it gets better