Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Nearby Locations with GeoFire

I'm having a difficult time understanding how GeoFire queries nearby locations;

I'm building a geo-based app that will fetch nearby locations based on the users location. My data is structured as follows

locations
    -Ke1uhoT3gpHR_VsehIv
    -Kdrel2Z_xWI280XNfGg
        -name: "place 1"
        -description: "desc 1"
geofire
    -Ke1uhoT3gpHR_VsehIv
    -Kdrel2Z_xWI280XNfGg
        -g: "dr5regw90s"
        -l
            -0: 40.7127837
            -1: -74.00594130000002

I can't seem to comprehend the whole idea of "tracking keys" and locations leaving & entering GeoQueries. (Perhaps this concept is more related to Uber-like functionality)

Assuming the locations above are nearby, how exactly would I use my own lat and long coordinates to fetch them? I'm sure I'm misinterpreting GeoFires documentation but I'm just not seeing it.

like image 786
Clay Banks Avatar asked Mar 04 '17 21:03

Clay Banks


People also ask

How do you use GeoFire?

Example UsageYou can store the location for each bar using GeoFire, using the bar IDs as GeoFire keys. GeoFire then allows you to easily query which bar IDs (the keys) are nearby. To display any additional information about the bars, you can load the information for each bar returned by the query at /bars/<bar-id> .

Does GeoFire work with firestore?

There is now a library for both iOS and Android that replicates GeoFire for Firestore. The library is called GeoFirestore. It has full documentation and is well tested. I currently use it in my app and it works brilliantly.

How do I store my geolocation on firestore?

Saving Geolocation Data in FirestoreYou name the object whatever you want and save multiple points on a single document. The library provides a the point method to help you create this data. If updating an existing doc, you can use setPoint(id, lat, lng) to non-destructively update the document.


1 Answers

To get all the locations which are nearby, first we get a Database reference to where we save our GeoFire locations. From your question, it should be

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("geofire");

Next we create a GeoFire instance with our GeoFire location reference as the argument

GeoFire geoFire = new GeoFire(ref);

Now we query the GeoFire reference,geoFire using it's queryAtLocation method The queryAtLoction method takes 2 arguments:a GeoLocation object and the distance range. So if we use 3 as the distance range, any location which is 3kilometers away from the user will show up in the onKeyEntered(...) method.

NB: The GeoLocation object takes 2 arguments: latitude and longitude. So we can use the user's latitude and longitude as the arguments.

        GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLatitde, userLongitude), 3);
        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
                //Any location key which is within 3km from the user's location will show up here as the key parameter in this method 
                //You can fetch the actual data for this location by creating another firebase query here
    Query locationDataQuery = new FirebaseDatabase.getInstance().child("locations").child(key);
    locationDataQuery..addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            //The dataSnapshot should hold the actual data about the location
    dataSnapshot.getChild("name").getValue(String.class); //should return the name of the location and dataSnapshot.getChild("description").getValue(String.class); //should return the description of the locations
                        }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onKeyExited(String key) {}

            @Override
            public void onKeyMoved(String key, GeoLocation location) {}

            @Override
            public void onGeoQueryReady() {
                //This method will be called when all the locations which are within 3km from the user's location has been loaded Now you can do what you wish with this data
            }

            @Override
            public void onGeoQueryError(DatabaseError error) {

            }
        });
like image 191
Aryeetey Solomon Aryeetey Avatar answered Sep 18 '22 12:09

Aryeetey Solomon Aryeetey