我正在尝试为Android应用程序添加地理围栏。我在日志中注意到以下内容:
07-04 11:41:19.201 1945-2271/com.google.android.gms.persistent W/GeofencerStateMachine: Ignoring addGeofence because network location is disabled.
我还注意到,当我在系统设置中启用“高精度”模式时,它可以工作(这被称为GPS,Wi-Fi,蓝牙或蜂窝网络)。它之前是“仅设备”模式,仅限GPS。
这是地理围栏的预期功能吗?如果用户的设备处于“高精度”模式,它们是否只能工作?
首先,High accuracy
模式对于位置的所有方面都是必要的,即使谷歌地图现在也使用这种模式。
因此,Geofencing将用户当前位置的意识与用户与可能感兴趣的位置的接近度的意识相结合。要标记感兴趣的位置,请指定其纬度和经度。要调整位置的接近度,请添加半径。纬度,经度和半径定义了地理围栏,在感兴趣的位置周围创建了一个圆形区域或围栏。这就是为什么必要的准确位置High accuracy
模式是必要的。这是官方的Doc
其次根据文档需要可靠的数据连接,因为Geofence依赖于它。根据this,它依赖于可靠的数据连接。
根据地理围栏的配置方式,它可以提示移动推送通知,触发文本消息或警报,在社交媒体上发送目标广告,允许跟踪车队,禁用某些技术或提供基于位置的营销数据。此外,关于地理围栏的细节请阅读this。
同时,建议您获得用户的高精度许可,以实现地理围栏目的。如果更改,您可以确保提供用户高精度的对话框,类似于现在的其他位置应用程序,如uber或ola等。
像这样的东西:
public void displayLocationSettingsRequest(final Activity activity, final int requestCode) {
final Task<LocationSettingsResponse> result = createLocationRequestForDialogDisplay(activity);
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
if(!InventaSdk.locUpdateStarted) {
try {
initAndCheckEligibility(); //start location updates when all settings are satisfied
} catch (SecurityException s) {
P2pLogHelper.e(TAG, s.getMessage());
}
}
// All location settings are satisfied. The client can initialize location
// requests here.
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
activity,
requestCode);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
});
}
private static Task<LocationSettingsResponse> createLocationRequestForDialogDisplay(Context context) {
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.setNeedBle(true);
builder.setAlwaysShow(true);
builder.addLocationRequest(createLocationRequest());
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
// https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
SettingsClient settingsClient = LocationServices.getSettingsClient(context);
return settingsClient.checkLocationSettings(locationSettingsRequest);
}
除此之外,如果您的用户有WiFi可用,则准确度更高。
启用Wi-Fi可以显着提高定位精度,因此如果关闭Wi-Fi,您的应用可能永远不会获得地理围栏警报,具体取决于几种设置,包括地理围栏的半径,设备型号或Android版本。从Android 4.3(API级别18)开始,我们添加了“Wi-Fi仅扫描模式”功能,允许用户禁用Wi-Fi,但仍能获得良好的网络位置。最好提示用户并为用户提供快捷方式,以便在禁用这两种模式时启用Wi-Fi或Wi-Fi扫描模式。使用SettingsClient可确保正确配置设备的系统设置,以实现最佳位置检测。
资料来源:https://developer.android.com/training/location/geofencing#Troubleshooting