【发布时间】: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