如何获取触发的 Geo-Fire 查询的密钥

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

我正在构建一个允许用户触发地理区域的应用程序,例如开车进入特定农场的周边。

我最初使用的是地理围栏 API,它很管用,但当位置精度下降时也不太管用。

我喜欢 Geo-fire 的想法,因为如果用户的位置进入指定区域,即使他们的位置精度不完全准确,它也会起作用,而且似乎比 Geo-fencing 快得多 API

无论如何,我对如何确定输入的地理位置有疑问,例如使用地理围栏 API,它会告诉我触发了哪个地理围栏。作为 Geo-fire,我只有在触发它后才会向我发送通知。

这是 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));
    }

这是地理查询

 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) {

                }
            });
        }

    }

"Entered" 触发器的输出

这告诉我,我生成的唯一键和我触发地理位置的坐标

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

如果这完全可以用 Geo-fire 做,请告诉我 或者关于去哪里获得答案的一些指导

我认为没有任何默认方法可以提供触发位置地理查询的方法。但在你的情况下,你可以通过这种方式找到触发位置:

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) {

            }
        });
    }