0
我是Swift 3的新手,我有一個問題是關於確定座標是否在附近,與用戶位置有關。之後,我想將附近的位置顯示到TableView中。如何將附近的位置設置爲從Firebase檢索到的TableView
這是我的火力地堡結構:
我的類看起來是這樣的:
class UITableViewControllerForShop: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate
這些變量是全球:
let databaseRef = FIRDatabase.database().reference().child("Butikker")
var retrieveData = [String:AnyObject]()
var distanceInMeters: CLLocationDistance!
在我的viewDidLoad功能我執行這個:
override func viewDidLoad() {
super.viewDidLoad()
databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! FIRDataSnapshot
let dictionary = snap.value as! [String: AnyObject]
self.retrieveData = ["latitude": dictionary["Latitude"] as! Double as AnyObject, "longtitude": dictionary["Longtitude"] as! Double as AnyObject]
}
})
}
要檢查用戶的位置,並將其與火力地堡的位置,我執行這個比較:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
print(location.coordinate.latitude, location.coordinate.longitude)
//My location
let myCoordinate = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
if self.retrieveData.isEmpty == false {
let shopCoordinate = CLLocation(latitude: retrieveData["latitude"]! as! CLLocationDegrees, longitude: retrieveData["longtitude"]! as! CLLocationDegrees)
print(shopCoordinate.coordinate.latitude, shopCoordinate.coordinate.longitude)
distanceInMeters = myCoordinate.distance(from: shopCoordinate)
print(distanceInMeters)
}
//Distance
}
我初始化我的tableview是這樣的:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
if distanceInMeters > 5000 {
cell.textLabel?.text = retrieveData["latitude"] as! String
}
return cell
}
這是我的問題:
- 我無法弄清楚如何檢查我的距離是否大於5000在locationManager函數內初始化。
- 我的字典retrieveData不會爲每個鍵存儲超過1個值,如果我想在桌面視圖中顯示附近項目列表,會導致問題。
- 花費時間去通過for循環,這使得一切都變得更加困難。
問題我想有一個答案:
1.How使用distanceInMeters它的LocationManager函數內部初始化之後?
2.如何確定是否有附近的東西放進我的TableView?