7

So, I have an Android app that lets users view posts within a certain radius of their current location. I am storing the posts using firebase, but I couldn't find a way to query for posts within a specific radius in firebase, so now I am thinking of using geofire, but I am not sure if there is a way to store a location alongside my firebase posts using geofire, so, it would seem to me like the only way to associate a location with a post is to have the post id (the one generated by firebase) be the key for the geofire location. Is this correct or is there a better way?

dramzy
  • 1,379
  • 1
  • 11
  • 25
  • 2
    Sounds about right. Most implementations keep the properties of the posts separate from the geodata, so that the geoqueries only deal with minimal data. See https://geofire.firebaseapp.com/ for an introduction. – Frank van Puffelen Nov 24 '15 at 16:05
  • Ok, so two queries would be required to retrieve the posts here. One using Geofire to find locations within a certain radius of the current location and retrieve their keys and another using Firebase to retrieve post info for all posts that match the IDs from the Geofire locations, correct? I was hoping to use one query, but it seems it'll have to be this way. – dramzy Nov 24 '15 at 16:17
  • 2
    The second one isn't strictly speaking a query. It's a direct lookup by ID. Given that Firebase works over an existing socket connection and you can pipeline them, these lookups are pretty fast. If not: start a new question with the code that is problematic and we can have a look. – Frank van Puffelen Nov 24 '15 at 16:23
  • Once I get the post IDs, how do I retrieve the post details in firebase? I am new to firebase and the documentation doesn't seem to show a straightforward way of doing this. I should probably ask this as a separate question, but it might be trivial. – dramzy Nov 25 '15 at 02:17
  • 1
    `ref.child("posts").child(postID).addListenerForSingleValueEvent()...`. See https://www.firebase.com/docs/android/guide/retrieving-data.html#section-reading-once – Frank van Puffelen Nov 25 '15 at 02:20
  • Does that mean I am making a call for each post I need to retrieve? – dramzy Nov 25 '15 at 02:24
  • 1
    Yes. But didn't we already cover that a few comments ago? – Frank van Puffelen Nov 25 '15 at 04:43
  • Well, we covered that a query and then a lookup are required to get post details based on current location, but I didn't realize that we need a lookup per post. I guess that's fine if they don't take too long. – dramzy Nov 25 '15 at 04:49
  • Firebase geofire-js : how to get list of keys of near by static(fixed) locations : http://stackoverflow.com/questions/43632746/firebase-geofire-js-how-to-get-list-of-keys-of-near-by-staticfixed-locations – Ankit Maheshwari Apr 26 '17 at 11:58

1 Answers1

0

Yes, that the correct way. You create a flat DB. One branch with post data and one branch with locations.

Seb
  • 500
  • 5
  • 13