【发布时间】:2020-02-24 22:57:37
【问题描述】:
我想要做的是使用 Cloud Firestore 中的 Geopoint 在离我当前位置最近的商店组织我的单元格
我已经查看了整个堆栈,但我找不到如何在 ViewController 中设置 tableview 以使用 Cloud Firestore 中的地理点显示离我当前位置最近的花店
这是我目前必须设置的数据以传递给 VC,以便它将数据从 Firestore 组织到离我当前位置最近的商店
下面是我的 Firestore 中的集合图像和我的 ViewController 的图像,以了解我的应用程序是如何设置的
import Foundation
import UIKit
class Store {
var id: String
var storeName: String
var imageUrl: String
var location: ??
init(id: String,
storeName: String,
imageUrl: String,
location:??) { //
self.id = id
self.storeName = storeName
self.imageUrl = imageUrl
self.location = location //
}
convenience init(dictionary: [String : Any]) {
let id = dictionary["id"] as? String ?? ""
let storeName = dictionary["storeName"] as? String ?? ""
let imageUrl = dictionary["imageUrl"] as? String ?? ""
let location = dictionary["location"] as? String ?? "" //
self.init(id: id,
storeName: storeName,
imageUrl: imageUrl,
location: location) //
}
}
import UIKit
import CoreLocation
import Firebase
import FirebaseFirestore
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
@IBOutlet weak var tableView: UITableView!
var stores: [Store] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
tableView.dataSource = self
tableView.delegate = self
fetchStores { (stores) in
self.stores = stores
self.tableView.reloadData()
}
}
func fetchStores(_ completion: @escaping ([Store]) -> Void) {
let ref = Firestore.firestore().collection("storeName")
ref.addSnapshotListener { (snapshot, error) in
guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
return
}
completion(snapshot.documents.compactMap( {Store(dictionary: $0.data())} ))
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self){
if CLLocationManager.isRangingAvailable() {
// do Stuff
}
}
}
}
}
【问题讨论】:
-
看起来
ViewController没有一个名为stores的属性。如果有,能否将其包含在代码中以便我们查看? -
抱歉错字刚刚更新了我的代码,现在它确实具有 stores 属性
标签: swift firebase uitableview google-cloud-firestore location