2016-04-10 4 views
0

Derzeit, wie meine Codes unten gezeigt, meine Tabelle zeigt alles von Firebase. Wie beschränke ich die Liste auf das, was in der Nähe ist?Liste in der Nähe von GeoFire Standorten in der Tabellenansicht

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() 
    }) 

Ich bin neu in iOS-Programmierung und der oben genannte Codes ist aus einem Tutorial, Ich weiß, ich brauche Verwendung von GeoFire der GFQuery Objekte zu machen, aber ich kann einfach nicht herausfinden, wo dies in meinem Code zu setzen. Danke im Voraus!

Antwort

0

Ich habe es herausgefunden. Hoffe jemand kann den besseren Weg vorschlagen, dies zu tun.

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() 

     }) 
    }) 
}