简体   繁体   English

继续触发GeoFence事件

[英]Continues Trigger GeoFence Event

GeoFence triggering issue GeoFence触发问题

I have setup geofence in my app, I am using IntetnService for the handle trigger event. 我在我的应用程序中设置了地理围栏,我将IntetnService用于句柄触发事件。

My issue is with trigger event many time. 我的问题是触发事件很多次。

Suppose i am at 1 Location inside Geofence, unfortunately some time enter or exit event trigger, Then i do like check the geoEvent.getTriggerdLocation() properties and check with geo fence radius, 假设我位于Geofence内的1 Location,不幸的是有一段时间进入或退出事件触发器,那么我确实喜欢检查geoEvent.getTriggerdLocation()属性并检查Geo围栏半径,

if the trigger location to geo fence location distance greater then geofecen radius then an then i will release my exit event functionality, 如果触发位置到地理围栏位置的距离大于地理地理半径,则我将释放退出事件功能,

but eventually geofence trigger event 2 3 km far even i already entered in fence and my above logic will fail. 但是即使我已经进入围栏,最终地理围栏触发事件2 3 km远,我的上述逻辑也会失败。 see snap 见快照

在此处输入图片说明

i want some solid fix for these. 我想要一些可靠的解决方案。

Location is on with High priority 位置优先

this will happening more when i will near to border of fence 当我接近栅栏的边界时,这种情况还会发生

Add geo fence list, as of now i am using only one fence. 添加地理围栏列表,截至目前,我仅使用一个围栏。

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());

PendidngIntentService PendidngIntentService

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

getGeofencingRequest() AND moGeofencePendingIntent 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 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);
}

} }

Permissions 权限

<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" />

i stuck in this issue since many days, please help to finish the issue. 我已经困扰了很多天了,请帮助完成该问题。

Here is the points you should keep in mind when use Geofence in Application 这是在Application使用Geofence时应记住的几点

From official documentation 从官方文件

  • Choose the optimal radius for your geofence 为您的地理围栏选择最佳半径

For best results, the minimium radius of the geofence should be set between 100 - 150 meters. 为了获得最佳结果,地理围栏的最小半径应设置为100-150米。 When Wi-Fi is available location accuracy is usually between 20 - 50 meters. 使用Wi-Fi时,定位精度通常在20-50米之间。 When indoor location is available, the accuracy range can be as small as 5 meters. 如果有室内位置,则精度范围可小至5米。 Unless you know indoor location is available inside the geofence, assume that Wi-Fi location accuracy is about 50 meters. 除非您知道地理围栏内有室内位置,否则请假定Wi-Fi位置精度约为50米。

When Wi-Fi location is not available (for example, when you are driving in rural areas) the location accuracy degrades. 当无法使用Wi-Fi定位时(例如,在乡村地区行驶时),定位精度会降低。 The accuracy range can be as large as several hundred meters to several kilometers. 精度范围可以高达几百米到几千米。 In cases like this, you should create geofences using a larger radius. 在这种情况下,应使用更大的半径创建地理围栏。

  • Use the dwell transition type to reduce alert spam 使用暂停过渡类型来减少警报垃圾邮件

If you receive a large number of alerts when driving briefly past a geofence, the best way to reduce the alerts is to use a transition type of GEOFENCE_TRANSITION_DWELL instead of GEOFENCE_TRANSITION_ENTER. 如果在短暂驶过地理围栏时收到大量警报,则减少警报的最佳方法是使用过渡类型GEOFENCE_TRANSITION_DWELL而不是GEOFENCE_TRANSITION_ENTER。 This way, the dwelling alert is sent only when the user stops inside a geofence for a given period of time. 这样,仅当用户在给定时间段内停在地理围栏内时,才会发送驻留警报。 You can choose the duration by setting a loitering delay. 您可以通过设置延迟时间来选择持续时间。

Found one patch to avoid unwanted or dummy events. 找到了一个补丁程序以避免不必要的事件或虚拟事件。

you can get location on trigger time 您可以获取触发时间的位置

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

now store your geo fence location lat long and radius. 现在存储您的地理围栏位置,长度和半径。 not get trigger lat long and check the distance between trigger location and geo fence location lat long. 不能将触发板拉得太长,并检查触发板位置和地理围栏位置之间的距离要长。 if the distance are less then radius it should be the enter and is distance is more then radius it should be exit event 如果距离小于半径,则应为输入;距离大于半径,则应为出口

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM