I'm making an app which collects info on restaurants from users as logs and shows the collected restaurants depending on location if another user requests them. The logging part is done. The app can receive input (photos, comments, ratings etc) on a restaurant by a user, receives the location data from Google maps, and saves all these data onto Firebase. So each of the restaurant data would have its longitude / latitude saved on Firebase. The problem is now showing nearby restaurants to other users when they request for it. I've got the Google Maps SDK part done, but sending out a query to Firebase to find the nearest restaurant(s) is not working out so well.
locationsRef.queryOrderedByChild("longitude").queryStartingAtValue(locationManager.location!.coordinate.longitude-1).queryEndingAtValue (locationManager.location!.coordinate.longitude+0.01).observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
//This part searches for restaurants within 0.01 degrees in longitude
locationsRef.queryOrderedbyChild("latitude").queryStartingAtValue(locationManager.location!.coordinate.latitude-1).queryEndingAtValue(locationManager.location!.coordinate.latitude+0.01).observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
// This part does the same for latitude (0.01 degrees is around 1km)
// Add name label UILabel here
self.eateryNameLabel.text = name
// Add address label UILabel here
self.eateryAddressLabel.text = address
var names = [String]()
var addresses = [String]()
var longs = [Double]()
var lats = [Double]()
for _ in snapshot.children {
var name = snapshot.value!["eateryname"] as! String
names.append(name)
var address = snapshot.value!["address"] as! String
addresses.append(address)
var long = snapshot.value!["longitude"] as! Double
longs.append(long)
var lat = snapshort.value!["latutide"] as! Double
lats.append(lat)
}
So this is what I've tried to do, but I cannot get it to run because I don't have a good understanding of retrieving data from Firebase yet. ' observeEventType(.ChildAdded' is also causing an ambiguity error. Is this the easiest way to do this? Am I missing any crucial lines?