这就是我创建地理围栏的方式:
ArrayList geofences = new ArrayList<>();
geofences.add(new Geofence.Builder()
.setRequestId(geofenceRequestID)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(
location.getLatitude(), location.getLongitude(), radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build());
其中:geofenceRequestID = "1";
,半径为:200;
这是我添加地理围栏的方式:
mGeofencingClient.addGeofences(getGeofencingRequest(location, geofences), getGeofencePendingIntent(context)).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
isSettingGeofence = false;
Utils.makeNotification(context, "GEOFENCE ADDED: " + location.getLatitude() + ", " + location.getLongitude(), true);
PSLocationService.getInstance(context).setTrackingEnabled(false);
Utils.appendLog(context, "Geofence Transitions Service setGeofenceRequest Success adding geofences!" +
location.getLatitude() +
" , " + location.getLongitude(),
"I",
Constants.GEOFENCE);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
isSettingGeofence = false;
Utils.appendLog(context, "Geofence Transitions Service setGeofenceRequest FAILURE adding geofences!" +
e.getMessage(),
"E",
Constants.GEOFENCE);
}
});
现在处理意图。我这样做:
Location triggering = geofencingEvent.getTriggeringLocation();
if(triggering != null) {
Utils.appendLog(PSGeofenceTransitionsIntentService.this, "GEOFENCE onHandleIntent got an EXIT transition: " + triggering.getLatitude() + ", " + triggering.getLongitude() + "/ " + triggering.getAccuracy(), "D", Constants.GEOFENCE);
}
我遇到了这个案子:
2017-10-17 14:38:32.615 30908:30908 [I] psngr.TRACKER.geofence Geofence Transitions Service setGeofenceRequest Success adding geofences! 52.37040665000001 , 4.9299061
2017-10-17 14:39:18.664 30908:8765 [D] psngr.TRACKER.geofence GEOFENCE onHandleIntent got an EXIT transition: 52.370363, 4.9306961/ 899.999
如果您检查它们之间的距离:https://www.google.nl/maps/dir/52.370363,+4.9306961/52.3704067,4.9299061/@52.370435,4.9300744,18.8z/data=!4m7!4m6!1m3!2m2!1d4.9306961!2d52.370363!1m0!3e3 或者使用:http://www.gpsvisualizer.com/calculators它会给你54米的距离。 现在我看到我的触发位置的准确性是899.999,但是地理围栏机制不应该考虑到这一点吗?这是一个错误,我需要以某种方式将其提升到谷歌吗?或者我做错了什么?