Reputation: 239
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?
Upvotes: 0
Views: 4827
Reputation: 11175
You can try .observeSingleEventOfType(FIRDataEventType.Value, ...
for that
Note, that it's not have built in loop, and not watching for changes in DB. You just get data, you need and that's all
Upvotes: 0
Reputation: 2182
I would have to see how you store it to the Firebase (how it looks in the DB on firebase)... if you have trouble getting data from firebase just print() the whole snapshot or anything you want to debugger and sooner or later you will find the right answer...
I am using this for similar retrieving of the data... just a note .ChildAdded has built in For in Loop
ref.child(user.uid).child("/markers").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
let objName = snapshot.key
print("key: \(objName)")
let tit = snapshot.value!["title"] as! String
let snip = snapshot.value!["snip"] as! String
let lat = snapshot.value!["lat"] as! Double
let long = snapshot.value!["long"] as! Double
let marker = GMSMarker()
marker.title = tit
marker.snippet = snip
marker.position = CLLocationCoordinate2DMake(lat, long)
marker.map = self.mapView
})
Upvotes: 2