【发布时间】:2017-11-18 10:27:54
【问题描述】:
不知道如何在用户附近自动对地址进行排序。我不明白从哪里开始以及如何编写代码。
我的 firebase 中有地址。地址具有字符串类型(不是纬度/经度)。示例:
"Москва, Электрозаводская, 21"
"Москва, Арбат, Староконюшенный переулок, дом 43"
"Москва, улица 1-я Бухвостова, дом 12/11, корпус 53"
我知道需要使用 firebase 中的 query,但是如何使用 query,我现在不明白。
这是我的代码:
class PhotoStudiosViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
@IBOutlet weak var tableView: UITableView!
var theStudios: [Studio] = []
var studiosRef: DatabaseReference!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
tableView.estimatedRowHeight = 475
tableView.rowHeight = UITableViewAutomaticDimension
loadDataFromFirebase()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return theStudios.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! PhotoStudiosTableViewCell
cell.addressLabel.text = theStudios[indexPath.row].studioAddress
return cell
}
func loadDataFromFirebase() {
studiosRef = Database.database().reference(withPath: "Addresses")
let time = DispatchTime.now() + 0.5
studiosRef.observe(.value, with: { (snapshot) in
DispatchQueue.main.asyncAfter(deadline: time) {
for imageSnap in snapshot.children {
let studioObj = Studio(snapshot: imageSnap as! DataSnapshot)
self.theStudios.append(studioObj)
}
self.tableView.reloadData()
}
})
}
class Studio {
var ref: DatabaseReference!
var studioAddress: String = ""
init(snapshot: DataSnapshot) {
ref = snapshot.ref
studioName = snapshot.key
let value = snapshot.value as! NSDictionary
studioAddress = value["address"] as? String ?? "---"
}
}
如何使用来自 firebase 的数据对用户附近的地址进行自动排序?
【问题讨论】:
标签: ios swift firebase firebase-realtime-database userlocation