0

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
sofarsophie
  • 239
  • 4
  • 20

2 Answers2

2

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

        })
Mazel Tov
  • 2,064
  • 14
  • 26
  • So it's a list of restaurants like this : "restaurants": { "1": { "eateryname": "-", "address": "-", "longitude": "-", "latitude": "-" }, "2": { ... }, "3": { ... } }, – sofarsophie Jul 21 '16 at 07:27
  • Then is lining up two queries in a row (to sort data using 2 factors, longitude and latitude) a good approach? – sofarsophie Jul 21 '16 at 07:29
  • are you sure that you will get some results from that query? I would try something similar to this though https://github.com/firebase/geofire but it might not run with the Firebase 3... I thought you had problem obtaining data... – Mazel Tov Jul 21 '16 at 07:44
0

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

Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
  • Hi, thanks for the answer. But I still get the "Type of expression is ambiguous without more context" error when I use ' (.Value '. Is there a way to solve this? What more information do I need to provide? – sofarsophie Jul 22 '16 at 04:18
  • @SeHyunPark It's buggy swift feature. Dot without anything before means the parameter is enum. If you are using Swift 3 you must use `.value`, if it's still not working, put enum type too `FIRDataEventType.Value` – Dmytro Rostopira Jul 22 '16 at 06:08