0

我是Swift 3的新手,我有一個問題是關於確定座標是否在附近,與用戶位置有關。之後,我想將附近的位置顯示到TableView中。如何將附近的位置設置爲從Firebase檢索到的TableView

這是我的火力地堡結構:

enter image description here

我的類看起來是這樣的:

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 
} 

這是我的問題:

  1. 我無法弄清楚如何檢查我的距離是否大於5000在locationManager函數內初始化。
  2. 我的字典retrieveData不會爲每個鍵存儲超過1個值,如果我想在桌面視圖中顯示附近項目列表,會導致問題。
  3. 花費時間去通過for循環,這使得一切都變得更加困難。

問題我想有一個答案:

1.How使用distanceInMeters它的LocationManager函數內部初始化之後?

2.如何確定是否有附近的東西放進我的TableView?

回答

0

如果你想做到這一點,我建議你使用一個庫,做你需要的東西:https://github.com/firebase/geofire-objc

我想你可以自己寫這一點,但它會佔用大量的時間,因爲火力地堡沒有幫助你有任何與GeoLocation相關的功能(如果你將它與Parse服務(dead)進行比較,它已經包含了通過距離查詢位置的功能)。