Android 更改位置准确性

Posted

技术标签:

【中文标题】Android 更改位置准确性【英文标题】:Android Change location accuracy 【发布时间】:2016-04-10 15:16:54 【问题描述】:

我想提示用户更改位置精度,例如谷歌地图。 我发现的选项是使用 Intent 将用户导航到设置以手动更改它。 但是谷歌地图的做法不同,它会提示您是否要切换到高精度,如果您单击“是”,它会更改此设置而不切换到“设置”应用程序。

我想知道你们中是否有人知道任何解决方案。 谢谢

【问题讨论】:

【参考方案1】:

这里是代码。

if (googleApiClient == null) 
            googleApiClient = new GoogleApiClient.Builder(getActivity())
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
                googleApiClient.connect();

                LocationRequest locationRequest = LocationRequest.create();
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                locationRequest.setInterval(30 * 1000);
                locationRequest.setFastestInterval(5 * 1000);
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                        .addLocationRequest(locationRequest);

                //**************************
                builder.setAlwaysShow(true); //this is the key ingredient
                //**************************

                PendingResult<LocationSettingsResult> result =
                        LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
                result.setResultCallback(new ResultCallback<LocationSettingsResult>() 
                    @Override
                    public void onResult(LocationSettingsResult result) 
                        final Status status = result.getStatus();
                        final LocationSettingsStates state = result.getLocationSettingsStates();
                        switch (status.getStatusCode()) 
                            case LocationSettingsStatusCodes.SUCCESS:
                                // All location settings are satisfied. The client can initialize location
                                // requests here.
                                break;
                            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                // Location settings are not satisfied. But could be fixed by showing the user
                                // a dialog.
                                try 
                                    // Show the dialog by calling startResolutionForResult(),
                                    // and check the result in onActivityResult().
                                    status.startResolutionForResult(
                                            getActivity(), 1000);
                                 catch (IntentSender.SendIntentException e) 
                                    // Ignore the 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;
                        
                    
                );             

【讨论】:

以上是关于Android 更改位置准确性的主要内容,如果未能解决你的问题,请参考以下文章

Android - 我如何在谷歌地图中实现位置准确性

Google Analytics for Android 的位置准确性

Android位置不准确

在 Android 11 (API 30) 上检测“提高位置准确性”开/关状态

如何最准确地确定 Android 设备的位置?

在android中获取当前准确位置的最佳方法是啥?