由于“网络位置已禁用”,因此无法在Android中添加Geofence

时间:2018-07-09 20:18:50

标签: android geofencing android-geofence

我正在尝试将地理围栏添加到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。

这是地理围栏的预期功能吗?仅当用户将设备置于“高精度”模式时,它们才能工作吗?

enter image description here

2 个答案:

答案 0 :(得分:3)

首先,High accuracy模式对于位置的各个方面都是必要的,即使Google Maps现在也使用此模式。

因此,地理围栏将对用户当前位置的了解与对用户可能感兴趣的位置附近的了解结合在一起。要标记感兴趣的位置,请指定其纬度和经度。要调整位置的接近度,请添加半径。纬度,经度和半径定义地理围栏,在感兴趣的位置周围创建圆形区域或围栏。这就是为什么必须使用其精确定位High accuracy模式的原因。这是官方的Doc

其次,根据文档要求,由于Geofence依赖可靠的数据连接。根据{{​​3}},它取决于可靠的数据连接。

根据地理围栏的配置,它可以提示移动推送通知,触发文本消息或警报,在社交媒体上发送目标广告,允许跟踪车队,禁用某些技术或提供基于位置的营销数据。此外,有关地理围栏的详细信息,请阅读this

答案 1 :(得分:0)

但是,出于地理栅栏目的,建议您从用户那里获得“高精度”权限。更改后,您可以确保向用户提供高准确度的对话框,类似于到目前为止的其他位置应用(如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