I'm trying to search for nearby registered locations using GeoFire, and return a default value if there are none within the specified radius. My problem is that when GeoFire doesn't find anything nearby, it returns absolutely nothing. Is there a different way to do this?
let geoFire = GeoFire(firebaseRef: DataService().LOC_REF)
let myLoc = CLLocation(latitude: 10.500000, longitude: -100.916664)
let circleQuery = geoFire.queryAtLocation(myLoc, withRadius: 100.0)
circleQuery.observeEventType(GFEventType.KeyEntered, withBlock: {
(key: String!, location: CLLocation!) in
self.allKeys[key]=location
print(self.allKeys.count) //NOT GETTING HERE
print(key)
//from here i'd like to use the key for a different query, or determine if there are no keys nearby
}
})
})
Thanks in advance
This is how you can use those two functions to build a list of keys, and listen for when the list has completed being created.
func fetchLocations() {
keys = [String]()
let geoFire = GeoFire(firebaseRef: DataService().LOC_REF)
let myLoc = CLLocation(latitude: 10.500000, longitude: -100.916664)
let circleQuery = geoFire.queryAtLocation(myLoc, withRadius: 100.0)
// Populate list of keys
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
keys.append(key)
})
// Do something with list of keys.
circleQuery.observeReadyWithBlock({
print("All initial data has been loaded and events have been fired!")
if keys.count > 0 {
// Do something with stored fetched keys here.
// I usually retrieve more information for a location from firebase here before populating my table/collectionviews.
}
})
}