Search code examples
javaandroidgeolocationandroid-geofencegeofire

How to get the key of a triggered Geo-Fire Query


I am building an app that allows a user to trigger a Geo-graphical area, for example driving into the perimeter of particular farms.

I originally was using Geo-fencing API, which works, but also doesn't work so well when the location accuracy dips.

and I liked the idea of Geo-fire as it works if the user's location enter a designated area even if their location accuracy isn't totally accurate, and seems to work a lot faster than Geo-fencing API

anyways, I have a problem with how to determine what Geo-location have I entered, for instance with Geo-fencing API, it tells me which Geo-fence was triggered. where as Geo-fire I have only gotten it to send me a notification once I have triggered it.

here is the geo-locations that are in a class

public static final HashMap<String, Map.Entry<LatLng, Integer>> MAIN_GEOFENCES = new HashMap<>();
    static {
            MAIN_GEOFENCES.put("Ticket Office",new AbstractMap.SimpleEntry<LatLng, Integer>(new LatLng(-33.911024, 19.119688),GEOFENCE_RADIUS_IN_METERS_SMALL));
            MAIN_GEOFENCES.put("Maison", new AbstractMap.SimpleEntry<LatLng, Integer>(Maison,GEOFENCE_RADIUS_IN_METERS_SMALL));
            MAIN_GEOFENCES.put("R45", new AbstractMap.SimpleEntry<LatLng, Integer>(new LatLng(-33.890257, 19.087895),GEOFENCE_RADIUS_IN_METERS_SMALL));
            MAIN_GEOFENCES.put("Mont Rochelle", new AbstractMap.SimpleEntry<LatLng, Integer>(MontRochelle,150));
            MAIN_GEOFENCES.put("Holden Manz",new AbstractMap.SimpleEntry<LatLng, Integer>(HoldenManz,GEOFENCE_RADIUS_IN_METERS_SMALL));
            MAIN_GEOFENCES.put("TurnOffAtRobertsvlei", new AbstractMap.SimpleEntry<LatLng, Integer>(new LatLng(-33.918157, 19.120557),GEOFENCE_RADIUS_IN_METERS_SMALL));
            MAIN_GEOFENCES.put("ExcelsiorRoad", new AbstractMap.SimpleEntry<LatLng, Integer>(new LatLng(-33.926339, 19.120064),GEOFENCE_RADIUS_IN_METERS_SMALL));
            MAIN_GEOFENCES.put("Charmonix", new AbstractMap.SimpleEntry<LatLng, Integer>(Charmonix,130));
            MAIN_GEOFENCES.put("DieuDonne", new AbstractMap.SimpleEntry<LatLng, Integer>(DieuDonne,120));
            MAIN_GEOFENCES.put("Grande Provance Platform", new AbstractMap.SimpleEntry<LatLng, Integer>(GrandeProvancePlatform,30));
            MAIN_GEOFENCES.put("Grande Provance Wine", new AbstractMap.SimpleEntry<LatLng, Integer>(GrandProvanceWine,70));
            MAIN_GEOFENCES.put("Grande Provance Resturant", new AbstractMap.SimpleEntry<LatLng, Integer>(GrandProvanceResturant,100));
            MAIN_GEOFENCES.put("Rickety Bridge",new AbstractMap.SimpleEntry<LatLng, Integer>(RicketyBridge,120));
            MAIN_GEOFENCES.put("Rickety Bridge Platform",new AbstractMap.SimpleEntry<LatLng, Integer>(RicketyBridgePlatform,30));
            MAIN_GEOFENCES.put("PlatformA", new AbstractMap.SimpleEntry<LatLng, Integer>(PlatformA,30));
    }

And here is the Geo-Query

 private void GeoFence()
    {
        for (Map.Entry<String, Map.Entry<LatLng,Integer>> entry : Constants.MAIN_GEOFENCES.entrySet()) {
            GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(entry.getValue().getKey().latitude,entry.getValue().getKey().longitude),0.5f);
            geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {
                    TriggeredGeoFence(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
                    System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
                }

                @Override
                public void onKeyExited(String key) {
                    TriggeredGeoFence("Exited");
                }

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

                }

                @Override
                public void onGeoQueryReady() {

                }

                @Override
                public void onGeoQueryError(DatabaseError error) {

                }
            });
        }

    }

Output of the "Entered" trigger

this tells me, my Unique key that is generated and the Coordinates of where I triggered the geo-location

I/System.out: Key cDkrRkdTvBaJwnICeVmqtCvHDn92 entered the search area at [-33.911080,19.119722]

Please let me know if this is at all possible to do with Geo-fire or some direction on where to go and get answers


Solution

  • I don't think there is any default method that provides for which location geoquery triggered. But in your case you can find triggered location in this way:

    private void GeoFence()
        {
            for (Map.Entry<String, Map.Entry<LatLng,Integer>> entry : Constants.MAIN_GEOFENCES.entrySet()) {
                setGeoQuery(entry);
            }
    
        }
    
        private void setGeoQuery(Map.Entry<String, Map.Entry<LatLng,Integer>> entry){
            GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(entry.getValue().getKey().latitude,entry.getValue().getKey().longitude),0.5f);
            geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {
                    Log.e("triggered for "+entry.getKey(),"lat:"+entry.getValue().getKey().latitude+",lng:"+entry.getValue().getKey().longitude);
                    TriggeredGeoFence(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
                    System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
                }
    
                @Override
                public void onKeyExited(String key) {
                    TriggeredGeoFence("Exited");
                }
    
                @Override
                public void onKeyMoved(String key, GeoLocation location) {
    
                }
    
                @Override
                public void onGeoQueryReady() {
                 Log.e("triggered for "+entry.getKey(),"lat:"+entry.getValue().getKey().latitude+",lng:"+entry.getValue().getKey().longitude);
                }
    
                @Override
                public void onGeoQueryError(DatabaseError error) {
    
                }
            });
        }