在tableview中列出附近的GeoFire位置

时间:2016-04-10 13:40:03

标签: ios swift firebase geofire

目前,正如我的代码所示,我的tableview显示了Firebase的所有内容。如何将列表限制在附近?

DataService.dataService.BUSINESS_REF.observeEventType(.Value, withBlock: { snapshot in

        // A snapshot of the businesses data

        self.businesses = []

        if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {

            for snap in snapshots {

                // Make business array for the tableview
                if let postDictionary = snap.value as? Dictionary<String, AnyObject> {

                    let key = snap.key
                    let business = Business(key: key, dictionary: postDictionary)

                    // Show newest business first
                    self.businesses.insert(business, atIndex: 0)
                }
            }
        }

        // Update the table when there is new data

        self.searchTableView.reloadData()
    }) 

我是iOS编程的新手,上面的代码来自一个教程,我意识到我需要使用GeoFire的GFQuery对象,但我无法弄清楚这个放在哪里在我的代码中。提前谢谢!

1 个答案:

答案 0 :(得分:0)

想出来。希望有人可以建议更好的方法。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    firebase = Firebase(url:"https://myapp.firebaseio.com/")
    geoFire = GeoFire(firebaseRef: firebase!.childByAppendingPath("geo"))

    let userLocation:CLLocationCoordinate2D = (locationManager.location?.coordinate)!
    let center = CLLocation(latitude: userLocation.latitude, longitude: userLocation.longitude)
    let span = MKCoordinateSpanMake(0.0125, 0.0125)
    let region = MKCoordinateRegionMake(center.coordinate, span)
    let regionQuery = geoFire?.queryWithRegion(region)

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    regionQuery!.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in

        // Check for changes in Firebase database
        DataService.dataService.BUSINESS_REF.queryOrderedByKey().queryEqualToValue(key).observeEventType(.Value, withBlock: { snapshot in

            if let snapshots = snapshot.children.allObjects as? [FDataSnapshot]  {

                for snap in snapshots {

                    if let postDictionary = snap.value as? Dictionary<String, AnyObject> {

                    //let key = snap.key
                    let business = Business(key: key, dictionary: postDictionary)

                    self.businesses.insert(business, atIndex: 0)

                    }
                }
            }

            self.searchTableView.reloadData()

        })
    })
}