0

I've tried for so many weeks to get it to work. I have read the documentation many times and there seem to be no examples of this online.

Goal: All I want to be able to do is print to the console all the posts that are within a 10km radius of the current user's location.

Issues: I don't really understand what parameters need to placed into the geoFire.setLocation the user's locations or post location. The documentation only shows a manually entered coordinates. I want to pull mine from firebase and query it for when the user gets within 10km they are printed to the console. Currently, with the code I have nothing is being printed at all.

fileprivate func setupGeoFireLocation() {

    let ref = Database.database().reference(withPath: "posts")
    ref.observe(.childAdded, with: { (snapshot) in

        guard let dictionary = snapshot.value as? [String: Any] else { return }

        guard let latitude = dictionary["latitude"] as? String else { return }
        guard let longitude = dictionary["longitude"] as? String else { return }

        let postLat = (latitude as! NSString).doubleValue
        let postLon = (longitude as! NSString).doubleValue


        self.geoFire.setLocation(CLLocation(latitude: postLat, longitude: postLat), forKey: "posts")
        //Not quite sure what's meant to be the "forKey:" parameter.

})
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation: CLLocation = locations[0] as CLLocation

    ref = Database.database().reference()
    geoFire = GeoFire(firebaseRef: ref)

    let center = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
     let circleQuery = geoFire.query(at: center, withRadius: 10.0)
    _ = circleQuery.observe(.keyEntered, with: { (key, location) in
        print(key)
    })

    circleQuery.observeReady{
        print("All initial data has been loaded and events have been fired for circle query!")
    }
}

Firebase Database

user
  • 345
  • 2
  • 8
  • 32
  • 2
    Are you using iOS simulator ? – Artem Nov 06 '18 at 04:15
  • Using my physical device – user Nov 06 '18 at 04:34
  • Do I need to set location still even tho I have coordinates saved in my database? because that may be the issue I didn't use `geoFire.setlocation` @Artem – user Nov 06 '18 at 04:34
  • 1
    For GeoQueries to work you need to indeed set the location in GeoFire as well. GeoFire queries a separate location, where it associates keys with geohashes of the location info. Please follow the GeoFire documentation on [setting a location for a key](https://github.com/firebase/geofire-objc#setting-location-data), [retrieving a location](https://github.com/firebase/geofire-objc#retrieving-a-location), and [querying](https://github.com/firebase/geofire-objc#geo-queries). – Frank van Puffelen Nov 06 '18 at 04:54

1 Answers1

0

From your comments it seems like you never specifically set the location information through GeoFire's setLocation method. For GeoQueries to work you need to indeed set the location in GeoFire as well. It won't simply work from your existing lat and lon values.

GeoFire queries a separate location, where it associates keys with geohashes of the location info. Please follow the GeoFire documentation on setting a location for a key, retrieving a location, and querying.

I also recommend reading these questions that explain more about GeoFire and how it works:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I've read through the documentation. Still quite unsure. Do I need to set the location to the post location or the users? and is the key just "posts" since that's my database name? – user Nov 06 '18 at 04:59
  • GeoFire associates a key with a location. The key is whatever (string) value you choose. If you're placing a post at a location, you'd use the key of the post (`-L...`). If you're placing a user at a location, use the ID of the user (`xgac...`). You should initialize GeoFire on a key that **it** manages completely, so that doesn't contain other data yet. – Frank van Puffelen Nov 06 '18 at 13:54