这是我在这个令人敬畏的小组中的第二篇文章,我在过去的5天里一直在研究如何从Firestore
获取数据并将其与我当前的用户location
进行比较。除非我实时测试我的应用程序(使用我的iPhone),否则一切似乎都运行良好。有时它会显示正确的位置,有时会崩溃或显示随机位置。我正在使用where()
方法从我的Firestore
访问数据,似乎它正在返回我需要的东西。我觉得我的文档中的名字在我访问信息时无法正常工作。
这是我的代码:
Firebase截图:
//Creating access to locationManager
var locationManager : CLLocationManager!
@IBOutlet weak var latLabel: UILabel!
@IBOutlet weak var lonLabel: UILabel!
@IBOutlet weak var place: UILabel!
@IBOutlet weak var placeImage: UIImageView!
//Storing the pass data that we got from the firt View
var placeName = String()
var latStore = String()
var lonStore = String()
var lonNumberStore = Double()
var latNumberStore = Double()
var fireLonMax = Double()
var fireLatMax = Double()
var fireLonMin = Double()
var fireLatMin = Double()
override func viewDidLoad() {
//Here goes some code to display on the SecondViewController
latLabel.text = latStore
lonLabel.text = lonStore
latMaxRead()
latMinRead()
lonMaxRead()
lonMinRead()
}
//This is my button to test if I am in the correct place
@IBAction func updateLocation(_ sender: UIButton) {
//
if (fireLatMin...fireLatMax).contains(latNumberStore) && (fireLonMin...fireLonMax).contains(lonNumberStore){
print("Is good place",fireLonMin,fireLonMax,fireLatMin,fireLatMax)
place.text = placeName
} else {
print("Is not good place", fireLonMin,fireLonMax,fireLatMin,fireLatMax)
place.text = "Bad"
}
}
func latMaxRead() {
let docRef = Firestore.firestore()
docRef.collection("places")
.whereField("latMax", isGreaterThanOrEqualTo: latNumberStore)
.getDocuments { (snapshot, error) in
if error != nil {
print("Error getting documents: \(String(describing: error))")
} else {
for document in (snapshot?.documents)! {
self.fireLatMax = document.data()["latMax"] as! Double
//This is where I pull the placeName on my Firebase
self.placeName = document.data()["placeName"] as! String
print("Fire latMax:", self.fireLatMax)
}
}
}
}
func latMinRead() {
let docRef = Firestore.firestore()
docRef.collection("places")
.whereField("latMin", isLessThanOrEqualTo: latNumberStore)
.getDocuments { (snapshot, error) in
if error != nil {
print("Error getting documents: \(String(describing: error))")
} else {
for document in (snapshot?.documents)! {
self.fireLatMin = document.data()["latMin"] as! Double
self.placeName = document.data()["placeName"] as! String
print("Fire latMin: ", self.fireLatMin)
}
}
}
}
func lonMaxRead() {
let docRef = Firestore.firestore()
docRef.collection("places")
.whereField("lonMax", isGreaterThanOrEqualTo: lonNumberStore)
.getDocuments { (snapshot, error) in
if error != nil {
print("Error getting documents: \(String(describing: error))")
} else {
for document in (snapshot?.documents)! {
self.fireLonMax = document.data()["lonMax"] as! Double
self.placeName = document.data()["placeName"] as! String
print("Fire lonMax: ", self.fireLonMax)
}
}
}
}
func lonMinRead() {
let docRef = Firestore.firestore()
docRef.collection("places")
.whereField("lonMin", isLessThanOrEqualTo: lonNumberStore)
.getDocuments { (snapshot, error) in
if error != nil {
print("Error getting documents: \(String(describing: error))")
} else {
for document in (snapshot?.documents)! {
self.fireLonMin = document.data()["lonMin"] as! Double
self.placeName = document.data()["placeName"] as! String
print("Fire lonMin : ", self.fireLonMin)
}
}
}
}
我觉得并且我非常自信我做错了,无论是我的查询还是我的地名。
我的控制台和模拟器的结果:
Result from my console and my simulator
我认为where()
方法是没有弄乱我的结果但是我不太舒服。
如果您在某种情况下将位置与某个半径进行比较,则无法正常工作。你应该使用Firebase GeoFire
代替。
GeoFire
正在进入FireStore
,一旦我与他们的家伙说话,但现在它不可能与FireStore
。但你可以做的只是将你的位置数据放在Firebase
中,在过滤了位置后,你可以再次在FireStore
上查询更多数据。
你可以从GeoFire
,GeoFire Doc和GeoFire Git得到一些关于GeoFire Blog的帮助。