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

时间:2019-08-29 21:00:44

标签: java android geolocation android-geofence geofire

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

我最初使用的是Geo-fencing API,它可以工作,但是当位置精度下降时,效果也不好。

我喜欢地理发射的想法,因为即使用户的位置准确度并不完全准确,如果用户的位置进入指定区域,它也可以工作,并且比地理围栏API的运行速度快很多

无论如何,我在确定输入的地理位置时遇到问题,例如使用Geo-fencing API,它告诉我触发了哪个Geo-fence。作为Geo-fire的地方,我只有在触发它后才能向我发送通知。

这是一个类别中的地理位置

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

                }
            });
        }

    }

“已输入”触发器的输出

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

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

请让我知道这是否与Geo-fire完全相关 或在何处获得答案的一些方向

1 个答案:

答案 0 :(得分:1)

我认为没有任何默认方法可提供针对地理位置查询触发的位置。但是您可以通过这种方式找到触发的位置:

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

            }
        });
    }