FusedLocationProviderClient 有时会提供不同的位置

4
 public void getDeviceLocation() {
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(getMvpView().getActivity());
        settingsClient = LocationServices.getSettingsClient(getMvpView().getActivity());
        createLocationCallback();
        createLocationRequest();
        buildLocationSettingsRequest();
        startLocationUpdates();
    }


    private void createLocationRequest() {
        locationRequest = new LocationRequest();
        locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }


    private void createLocationCallback() {
        getMvpView().showProgressDialog();
        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                Location currentLocation = locationResult.getLastLocation();
                getMvpView().showSelectedAddress(getAddressFromLatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
                fusedLocationClient.removeLocationUpdates(locationCallback);
                getMvpView().hideProgressDialog();
            }
        };
    }


    private void buildLocationSettingsRequest() {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(locationRequest);
        locationSettingsRequest = builder.build();
    }


    private void startLocationUpdates() {
        settingsClient.checkLocationSettings(locationSettingsRequest)
                .addOnSuccessListener(getMvpView().getActivity(), locationSettingsResponse -> {

                    if (ActivityCompat.checkSelfPermission(getMvpView().getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getMvpView().getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    fusedLocationClient.requestLocationUpdates(locationRequest,
                            locationCallback, Looper.myLooper());

                }).addOnFailureListener(getMvpView().getActivity(), e -> {
            getMvpView().hideProgressDialog();
            getMvpView().showErrorToast(R.string.please_enable_location);
        });
    }


    @SuppressLint("MissingPermission")
    private void startLocationUpdates() {
        settingsClient.checkLocationSettings(locationSettingsRequest)
                .addOnSuccessListener(getMvpView().getActivity(), locationSettingsResponse -> {
                    fusedLocationClient.requestLocationUpdates(locationRequest,
                            locationCallback, Looper.myLooper());

                }).addOnFailureListener(getMvpView().getActivity(), e -> {
            getMvpView().hideProgressDialog();
            getMvpView().showErrorToast(R.string.please_enable_location);
        });
    }


    private String getAddressFromLatLng(double latitude, double longitude) {
        Geocoder geocoder = new Geocoder(getMvpView().getActivity(), Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = geocoder.getFromLocation(
                    latitude,
                    longitude,
                    1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Address address = addresses.get(0);
        StringBuilder addressStringBuilder = new StringBuilder();
        for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
            addressStringBuilder.append(address.getAddressLine(i));
        }

        return addressStringBuilder.toString();
    }

我已经将纬度和经度转换为地址文本,但在我再次查询位置后,即使我没有移动我的当前位置,并在手机设置中启用了精度,地址也会发生更改。问题是,当我总是查询时,如果我的纬度和经度位置没有改变,它应该给我相同的位置地址。


GPS定位的一些波动是正常的,可能会出现几米甚至十几米的误差。而基于网络(蜂窝塔)的定位精度更低,所以可能导致位置信息发生较大变化。因此,也许地理编码服务本身没有问题,但实际坐标发生了足以影响结果的变化。 - Markus Kauppinen
1个回答

4

FusedLocationProviderClient指的是将GPS和通过Google将接收到的WLAN手机塔信号转换为经度纬度结合起来。

将接收到的WLAN手机塔信号转换为经度纬度是一个不太准确的启发式方法,其中还涉及其他因素,比如您的手机连接到哪个手机塔(以及Google每个塔的经纬度)以及最强的3个电话塔的信号有多强。

如果您的手机更改了手机塔连接,则对于Google的算法而言,您的手机位置已经发生了变化。

如果您的手机可以接收超过3个手机塔,则最强的3个手机塔也会根据每个塔的流量变化而变化。

GPS接收器的成本很高。当启用GPS节能优化时,可能会出现GPS精度问题,导致融合定位跳动。

我第一次使用全新的手机进行地理缓存时发现了这个问题,惊奇地发现自己的位置在地图上跳动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接