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!")
}
}