我怎样才能在我的TBV牢房中显示预计到达时间(ETA)?
我使用CloudFiRestore来存储我在单元格中列出的所有存储的数据。Gepoint在我的“位置”字段中用作类型,用于为FiRestore设置纬度和经度。这样我就能画出商店的位置。
我在viewDidLoad中也有一个函数,它为离你最近的商店安排单元格。
我试图让etaLabels在细胞里显示ETA离你有多远,通过开车/汽车距离你现在的位置有几分钟的距离。而不是它必须显示你从离你最近的商店的距离。
import UIKit import CoreLocation import Firebase import FirebaseFirestore
class StoreController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
@IBOutlet weak var storeTableView: UITableView!
var storeSetup: [StoreList] = []
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
storeTableView.dataSource = self
storeTableView.delegate = self
// arranges cells to the stores nearest your current location
fetchStores { (products) in
self.storeSetup = products.sorted(by: { $0.distanceFromUser < $1.distanceFromUser })
self.storeTableView.reloadData()
}
}
func fetchStores(_ completion: @escaping ([StoreList]) -> Void) {
let ref = Firestore.firestore().collection("stores")
ref.addSnapshotListener { (snapshot, error) in
guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
return
}
completion(snapshot.documents.compactMap( {StoreList(dictionary: $0.data())} ))
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self){
if CLLocationManager.isRangingAvailable() {
}
}
}
}
}
extension StoreController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storeSetup.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as?
StoreCell else { return UITableViewCell() }
cell.configure(withStores: storeSetup[indexPath.row])
return cell
}
} import UIKit import SDWebImage import Firebase
class StoreCell: UITableViewCell {
weak var stores: StoreList!
@IBOutlet weak var storeImage: UIImageView!
@IBOutlet weak var storeName: UILabel!
@IBOutlet weak var etaLbl: UILabel!
@IBOutlet weak var categoryLbl: UILabel!
//func used to populate cells of tableview in StoreVC
func configure(withStores stores: StoreList) {
storeName = stores.storeName
categoryLabel.text = stores.category
productImage.sd_setImage(with: URL(string: stores.imageUrl))
etaLbl.text = "\(store. distanceFromUser)"
self. stores = stores
}
} import Foundation import UIKit import CoreLocation import Firebase import FirebaseFirestore
class StoreList { var id: String var name: String var storeName: String var imageUrl: String var location: CLLocationCoordinate2D
var distanceFromUser: Double
var selectedOption: Int
init(id: String,
storeName: String,
category: String,
imageUrl: String,
location: CLLocationCoordinate2D) {
self.id = id
self. storeName = storeName
self.category = category
self.imageUrl = imageUrl
self.location = location
self.distanceFromUser = (CLLocationManager().location?.distance(from: CLLocation(latitude: location.latitude, longitude: location.longitude)))!
print("look here!", self.distanceFromUser)
}
convenience init(dictionary: [String : Any]) {
let id = dictionary["id"] as? String ?? ""
let storeName = dictionary["storeName"] as? String ?? ""
let category = dictionary["category"] as? String ?? ""
let imageUrl = dictionary["imageUrl"] as? String ?? ""
let geoPoint = dictionary["location"] as! GeoPoint
let latitude = geoPoint.latitude
let longitude = geoPoint.longitude
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.init(id: id,
storeName: storeName,
category: category,
imageUrl: imageUrl,
location: location)
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。