【问题标题】:Is it possible to get information about an object from a Geofence?是否可以从地理围栏获取有关对象的信息?
【发布时间】:2021-09-14 15:29:59
【问题描述】:

我想知道是否可以得到一些帮助,我正在创建一个使用点对象来创建路线的应用程序,我在每条路线上都有某些点作为“航路点”,并且有了它,所以这些点中的每一个都可以绑定到地理围栏。

到目前为止,我可以触发地理围栏事件等,但我想(如果可能)获取有关实际“点”的信息,例如它的描述等以显示在另一个活动中,地理围栏的意思是代表,我不确定如何实现这一点,我似乎只能获得地理围栏的 ID,任何帮助将不胜感激!

这是我的 BroadcastReceiver 类


public class GeofenceBroadcastReceiver extends BroadcastReceiver {

    private static final Object TAG = "Error";
    NotifactionHelper notifactionHelper;

    @Override
    public void onReceive(Context context, Intent intent) {

        notifactionHelper = new NotifactionHelper(context);

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        if (geofencingEvent.hasError()) {
            Log.d("geoferror", "onReceive: Geofence even has error..");
        }

        List<Geofence> triggeredGeofenceList = geofencingEvent.getTriggeringGeofences();

        for (Geofence geofence : triggeredGeofenceList) {
            Log.d("GEOF", "onReceive: "+geofence.getRequestId());
        }

        Location triggerLocation = geofencingEvent.getTriggeringLocation();
        double lat = triggerLocation.getLatitude();
        double lon = triggerLocation.getLongitude();

        
        int transitionType = geofencingEvent.getGeofenceTransition();

        switch (transitionType) {

            case Geofence.GEOFENCE_TRANSITION_ENTER:
                Toast.makeText(context, "Entered Geofence", Toast.LENGTH_SHORT).show();
                Log.d("GEOF", "onReceive: "+geofencingEvent.getGeofenceTransition());
                notifactionHelper.sendNotification("A Waypoint is Nearby!", "Open the Application to find out more", RouteActivity.class);
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
                Toast.makeText(context, "Dwelling inside of Geofence", Toast.LENGTH_SHORT).show();
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                Toast.makeText(context, "Exited Geofence area", Toast.LENGTH_SHORT).show();
                break;



        }

        Bundle b = intent.getExtras();

        Intent i = new Intent("TESTER");
        i.putExtra("lat", lat);
        i.putExtra("lon", lon);
        i.putExtras(b);
        
        context.sendBroadcast(i);


    }

}

也是我的地理围栏方法,它从列表中的每个点获取纬度和经度来创建地理围栏

private void addGeoFence(LatLng latLng, float radius) {

        List<Geofence> geofence = geoFenceHelper.getGeoFence(GEOFENCE_ID, latLng, radius,
                Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT);

        GeofencingRequest geofencingRequest = geoFenceHelper.getGeoFencingRequest(geofence);

        PendingIntent pendingIntent = geoFenceHelper.getPendingIntent();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        geofencingClient.addGeofences(geofencingRequest, pendingIntent)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull @NotNull Exception e) {

                    }
                });
    }

还有我的 geofenceHelper 类

public class GeoFenceHelper extends ContextWrapper {

    private static final java.util.UUID UUID = null;
    PendingIntent pendingIntent;

    /** Constructor
     *
     * @param base
     */
    public GeoFenceHelper(Context base) {
        super(base);
    }

    public GeofencingRequest getGeoFencingRequest(List<Geofence> geofence) {

        return new GeofencingRequest.Builder()
                .addGeofences(geofence)
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
                .build();

    }

    /**
     *
     * @param ID
     * @param latLng
     * @param radius
     * @param transitionTypes
     * @return
     */
    public List <Geofence> getGeoFence (String ID, LatLng latLng, float radius, int transitionTypes) {

        List<Geofence> returnGeoFenceList = new ArrayList<>();
        String id = UUID.randomUUID().toString();

        returnGeoFenceList.add(
                new Geofence.Builder()
                        .setCircularRegion(latLng.latitude, latLng.longitude, radius)
                        .setRequestId(id)
                        .setTransitionTypes(transitionTypes)
                        .setLoiteringDelay(2000)
                        .setExpirationDuration(Geofence.NEVER_EXPIRE)
                        .build());

        return returnGeoFenceList;

    }

    /**
     *
     * @return
     */
    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;
    }

}

感谢您抽出宝贵时间阅读本文!

【问题讨论】:

  • such as its description etc 请说到重点。究竟是什么?准确!
  • 抱歉,每个点都是一个对象,包含它的 LatLng、点的字符串描述和枚举 PointType,当地理围栏被触发时,我希望我的活动向我展示更多关于点的信息我已经达到了,比如它的描述和 LatLng 在另一个活动中。
  • 您甚至没有解释“描述”的含义。是你解释了“等”。
  • 再次抱歉.. 'description' 是 Point 对象的实例变量,可用于添加包含有关特定 Point 的更多信息的描述。

标签: android broadcastreceiver android-geofence


【解决方案1】:

将有关位置的数据保存在数据库中,包括点的 id。通过传递给您的 id 查找数据。地理围栏本身不应该保存这些数据,但它应该与数据相关联。

【讨论】:

  • 谢谢 Gabe,目前我将每条路线的信息存储在 Firestore 中,每个点都是每条路线中 Point 类型列表的一部分,尽管我没有给每个点一个唯一的 id
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 2011-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多