开发者社区> 问答> 正文

如何使用cloud Firestore在Tableview单元格中显示ETA?

我怎样才能在我的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)
}

}

展开
收起
游客5akardh5cojhg 2019-12-07 19:17:07 486 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载