首页 > 解决方案 > 在 BroadcastReceiver(多边形地理围栏)中使用 containsLocation

问题描述

我是编程和这个社区的新手,但我们可以实现GoogleMaps.Util.PolyUtil- BroadcastReceiver 中的 containsLocation 方法吗?

<仅供参考>

我做了2个演示应用,第一个是学习Polygon(我们称之为appsA)。AndroidCoding 的代码作为参考https://www.youtube.com/watch?v=_Sx45CbBsoA

第二个是学习地理围栏(appsB)。我在这里使用 yoursTRULY 的代码https://www.youtube.com/watch?v=nmAtMqljH9M&list=PLdHg5T0SNpN3GBUmpGqjiKGMcBaRT2A-m&index=10

< /仅供参考>

问题:

我正在尝试从 appsA 实现 containsLocation ... https://i.stack.imgur.com/AlWVo.png 其中 currLoc 是 LastLocation 的 LatLng,而 latLngList 是多边形的 ArrayList。

进入appsB中的BroadcastReceiver ... https://i.stack.imgur.com/GMQvt.png

我已经尝试搜索有关此的解决方案,但没有一个有效/相关.. 我正在考虑将 Polygon LatLng 的 ArrayList 传递到 BroadcastReceiver -> 获取当前位置坐标 -> 然后最后使用 containsLocation

这是代码

在 MapsActivity 上初始化变量

    List<LatLng> latLngList = new ArrayList<>();
    List<Marker> markerList = new ArrayList<>();

地图活动

Geofence geofence = geofenceHelper.getGeofence(GEOFENCE_ID, latLng, radius, Geofence.GEOFENCE_TRANSITION_ENTER |
                Geofence.GEOFENCE_TRANSITION_DWELL);
        GeofencingRequest geofencingRequest = geofenceHelper.getGeofencingRequest(geofence);

        PendingIntent pendingIntent = geofenceHelper.getPendingIntent();

地理围栏助手

    public PendingIntent getPendingIntent(){
        if(pendingIntent!= null){
            return pendingIntent;
        }

        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 2607, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

广播接收器

        int transitionType = geofencingEvent.getGeofenceTransition();

        switch(transitionType){
            case Geofence.GEOFENCE_TRANSITION_ENTER:
//                it should look like this
//                boolean contains = PolyUtil.containsLocation(currLoc, latLngList, true );
//                if(contains)
                Toast.makeText(context, "GEOFENCE TRANSITION ENTER", Toast.LENGTH_SHORT).show();
                break;

我想问的是:

  1. 我可以将 ArrayList 传递给 BroadcastReceiver 吗?
  2. 有可能这样做吗?或者有更好的解决方案吗?

参考 Marian 在Android 中的回答:Build Polygonal Shape Geofence

之前谢谢

标签: javaandroidbroadcastreceiverpolygongeofencing

解决方案


推荐阅读