【问题标题】:Continues Trigger GeoFence Event继续触发 GeoFence 事件
【发布时间】:2017-07-01 02:57:50
【问题描述】:

GeoFence 触发问题

我在我的应用中设置了地理围栏, 我正在使用 IntetnService 处理触发事件。

我的问题是多次触发事件。

假设我在地理围栏内的 1 个位置,不幸的是有时进入或退出事件触发器, 然后我确实喜欢检查 geoEvent.getTriggerdLocation() 属性并检查地理围栏半径,

如果触发位置到地理围栏位置的距离大于地理围栏半径,那么我将释放我的退出事件功能,

但最终地理围栏触发事件 2 3 公里远,即使我已经进入围栏并且我的上述逻辑将失败。看快照

我想要对这些进行一些可靠的修复。

位置以高优先级开启

当我靠近栅栏的边界时,这种情况会发生更多

添加地理围栏列表,目前我只使用一个围栏。

mGeofenceList.add(new Geofence.Builder().setRequestId(String.valueOf(loGeoFenceModels.liGeoFenceID))
                    .setCircularRegion(loGeoFenceModels.ldGeoLatitude, loGeoFenceModels.ldGeoLongitude,
                            loGeoFenceModels.lfRadius)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                    .build());

待处理的IntentService

moGeofencePendingIntent = getGeofencePendingIntent();
        LocationServices.GeofencingApi
                .addGeofences(moLocationClient, getGeofencingRequest(), moGeofencePendingIntent)
                .setResultCallback(this);

getGeofencingRequest() 和 moGeofencePendingIntent

private GeofencingRequest getGeofencingRequest() {
    return new GeofencingRequest.Builder().setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofences(mGeofenceList).build();
}



private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (moGeofencePendingIntent != null) {
        return moGeofencePendingIntent;
    }

    Intent intent = new Intent(moContext, GeofenceTransitionsIntentService.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent
    // back when calling addgeoFences()
    return PendingIntent.getService(moContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

GeofenceTransitionsIntentService.class

import java.util.ArrayList;
import java.util.List;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofenceStatusCodes;
import com.google.android.gms.location.GeofencingEvent;
import android.R.bool;
import android.app.IntentService;
import android.app.usage.UsageEvents.Event;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.text.TextUtils;

public class GeofenceTransitionsIntentService extends IntentService {

protected static final String TAG = "GeofenceTransitionsIS";

public GeofenceTransitionsIntentService() {
    super(TAG); // use TAG to name the IntentService worker thread
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

private static String getGeofenceTransitionDetails(GeofencingEvent event) {
    String transitionString = GeofenceStatusCodes.getStatusCodeString(event.getGeofenceTransition());
    List<String> triggeringIDs = new ArrayList<String>();
    for (Geofence geofence : event.getTriggeringGeofences()) {
        triggeringIDs.add(geofence.getRequestId());
    }
    return String.format("%s: %s", transitionString, TextUtils.join(", ", triggeringIDs));
}

@Override
protected void onHandleIntent(Intent intent) {

    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    Log.i(TAG, "Geofencing Event : " + event);
    if (event.hasError()) {
        Log.i(TAG, "GeofencingEvent Error : " + event.getErrorCode());
        return;
    }

    // Get the type of transition (entry or exit)
    if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) {
        Log.i(TAG, "GeofencingEvent Enter");
    }
    if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) {
        Log.i(TAG, "GeofencingEvent Exit");
    ?
    String description = getGeofenceTransitionDetails(event);
    Log.i(TAG, "GeofencingEvent description : " + description);
}

}

权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.hardware.location.gps" />

我在这个问题上卡了很多天,请帮助完成这个问题。

【问题讨论】:

  • 发布更多代码。
  • @kishorejethava 可以吗?
  • 你检查过Geofence请求ID吗?两者可能是不同的请求 ID。
  • 没有,到目前为止只有一个地理围栏,所以没有机会获得唯一的请求 ID。我也有打印日志。 @kishorejethava
  • 区域半径有多长?

标签: android google-maps geolocation location android-geofence


【解决方案1】:

以下是在Application 中使用Geofence 时应牢记的几点

来自官方documentation

  • 为您的地理围栏选择最佳半径

为获得最佳效果,应设置地理围栏的最小半径 100 - 150 米之间。当 Wi-Fi 可用时,位置精度为 通常在 20 - 50 米之间。当室内位置可用时, 精度范围可小至 5 米。除非你知道室内 位置在地理围栏内可用,假设 Wi-Fi 位置 精度约为 50 米。

当 Wi-Fi 位置不可用时(例如,当您在 在农村地区驾驶)定位精度下降。准确度 范围可以大到几百米到几公里。 在这种情况下,您应该使用更大的半径创建地理围栏。

  • 使用停留过渡类型减少警报垃圾邮件

如果您在短暂驶过某个路口时收到大量警报 地理围栏,减少警报的最佳方法是使用过渡 GEOFENCE_TRANSITION_DWELL 的类型而不是 GEOFENCE_TRANSITION_ENTER。这样,仅发送住宅警报 当用户在给定时间段内停留在地理围栏内时。你 可以通过设置徘徊延迟来选择持续时间。

【讨论】:

  • 1.为您的地理围栏选择最佳半径 - 它将超过 700 米。 2.使用停留过渡类型来减少警报垃圾邮件-我知道,但我希望在市区使用此功能。有时会发生当用户没有在栅栏内停下来只是穿过栅栏时。
  • 你试过半径在 100m 到 300m 之间吗?因为在我的情况下,我设置了 200m,所以它在 ExitEnter 之后触发了 5m 左右
  • 伙计,在我的情况下,我已经在围栏中,它会触发退出事件,即使我离围栏边界这么远(近 200 米)。
【解决方案2】:

找到一个补丁来避免不需要的或虚拟的事件。

您可以在触发时间获得位置

Location loTriggerGeoFenceLocation = moEvent.getTriggeringLocation();
long ldAccuracy = loTriggerGeoFenceLocation.getAccuracy();

现在存储您的地理围栏位置纬度和半径。 没有触发 lat long 并检查触发位置和地理围栏位置 lat long 之间的距离。如果距离小于半径,它应该是输入,距离大于半径,它应该是退出事件

 if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) {

    ldGeoLatLong = moSharedPreferenceManager.getGeoFenceTimeLocation();
    Location loExistGeoFenceLocation = new Location("");
    loExistGeoFenceLocation.setLatitude(ldGeoLatLong[0]);
    loExistGeoFenceLocation.setLongitude(ldGeoLatLong[1]);

    String lsRadius = moSharedPreferenceManager.getGeoFenceRadius();
    float lfRadius = Float.parseFloat(lsRadius);
        Log.i(TAG, "GeofencingEvent Exit");
        float lfDistance = loTriggerGeoFenceLocation.distanceTo(loExistGeoFenceLocation);
        if (lfDistance >= lfRadius && ldAccuracy <= 100f) {
            moSharedPreferenceManager.setGeoFenceExit(true);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多