【问题标题】:Unable to add Geofence in Android because "network location is disabled"无法在 Android 中添加地理围栏,因为“网络位置已禁用”
【发布时间】:2018-12-17 14:28:11
【问题描述】:

我正在尝试向 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。

这是地理围栏的预期功能吗?只有当用户的设备处于“高精度”模式时,它们才会起作用吗?

【问题讨论】:

标签: android geofencing android-geofence


【解决方案1】:

首先High accuracy模式对于位置的所有方面都是必要的,即使谷歌地图现在也使用这种模式。

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

其次,根据文档需要可靠的数据连接,因为地理围栏依赖于它。根据this,它依赖于可靠的数据连接。

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

【讨论】:

    【解决方案2】:

    同时,出于地理围栏的目的,建议您获得用户的高精度许可。如果发生更改,您可以确保向用户提供高精度对话框,类似于目前其他位置应用程序(如 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2017-06-23
      • 2016-05-08
      相关资源
      最近更新 更多