Geofire query multiple references

947 views Asked by At

I am making a taxi app and I use Geofire and Geoquery to get nearest drivers.

Now I have 3 types of cars "3 seats, 4 seats and 7 seats" and the user can switch between them, I want to change the query to get data from the correct reference.

My Code for reference and query:

refDrivers = FirebaseDatabase.getInstance().getReference("drivers_locations_" + car_seats + "seat");
geoFireDrivers = new GeoFire(refDrivers);
geoQuery = geoFireDrivers.queryAtLocation(new GeoLocation(31.2551039, 30.0162619), 3);

where car_seats is the chosen car type by the user.

1

There are 1 answers

0
Wilik On BEST ANSWER

To change the reference, you have to create a new instance of GeoFire and then query it again, just make sure to remove the listeners to the GeoQuery before attaching new listener.

There's no method to change the reference on the fly.

private GeoQuery geoQuery;

public void switchCarSeatType() {
    if (geoQuery != null) {
        // remove the previous listeners
        geoQuery.removeAllListeners();
    }
    setGeoQuery(car_seats);
}

private void setGeoQuery(int car_seats) {
    refDrivers = FirebaseDatabase.getInstance().getReference("drivers_locations_" + car_seats + "seat");
    geoFireDrivers = new GeoFire(refDrivers);
    geoQuery = geoFireDrivers.queryAtLocation(new GeoLocation(31.2551039, 30.0162619), 3);
    geoQuery.addGeoQueryEventListener( ... );
}