bbgg2017
bbgg2017

Reputation: 187

Firebase GeoFire, retrieve posts with a specific radius from my current position

It's possible to get all the GeoFire posts stored in the firebase db with a specific radius from my current position?

Example: if into the node of my db I have 10 posts and only 3 are valid from my current position with a radius of 0.5km I will get the 3 posts.

This is the structure of my DB:

{
  "posts" : {
    "-L9qTBD1WPgF0h2ungz-" : {
      "desc" : "lorem ipsum",
      "isActive" : true,
      "idGeofire" : "ID1GEOFIRE"
    },
    "-L9qTDImKE4HHgxH5YDs" : {
      "desc" : "lorem ipsum",
      "isActive" : true,
      "idGeofire" : "ID2GEOFIRE"
    }
  },
  "geofire" : {
    "ID1GEOFIRE" : {
      "IdPost" : "L9qTBD1WPgF0h2ungz",
      "l" : {
        "0" : 46.632347,
        "1" : 3.6354347
      }
    },
    "ID2GEOFIRE" : {
      "IdPost" : "L9qTDImKE4HHgxH5YDs",
      "l" : {
        "0" : 25.6314647,
        "1" : 50.6369447
      }
    },
  }
}

Upvotes: 2

Views: 586

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

To get the nodes within a certain range of a location, you'll use a geoquery as shown in the documentation:

// creates a new query around [37.7832, -122.4056] with a radius of 0.6 kilometers
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(37.7832, -122.4056), 0.6);

Then you'll receive onKey... events for the nodes in range, e.g. for each post you'll get a onKeyEntered:

geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(String key, GeoLocation location) {
        System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
    }

Upvotes: 2

Related Questions