FusedLocationProviderClient有时会提供不同的位置

时间:2018-12-07 09:30:34

标签: android android-location android-fusedlocation

 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();
    }

我已将经纬度转换为地址文本,但是在我再次查询位置后,即使我没有从当前位置移开,地址也发生了变化,这也启用了电话设置的准确性。问题是当我始终查询时,如果我的经纬度位置不变,应该给我相同的位置地址。

1 个答案:

答案 0 :(得分:3)

FusedLocationProviderClient意味着将gps和google-translate-received-wlan-cellphone-tower-signals-to-lat-lon结合起来。

google-translate-received-wlan-celler-tower-signals-to-lat-lon是一种不精确的启发式方法,其除其他因素外还取决于您的手机连接到哪个手机塔(以及google在哪里经纬度(每个塔楼)以及最坚固的3个手机塔的坚固程度。

如果您的手机发生了变化,那就是手机与塔的连接,那么对于googl-s算法,您的手机位置也发生了变化。

如果您的手机可以接收3个以上的手机发射塔,则最强的3个手机发射塔也会随之变化,具体取决于每个发射塔的通信量。

GPS接收器消耗大量能量。启用gps能量优化后,可能还会有gps精度问题导致融合位置跳动。

我第一次使用全新的Cellpone进行地理缓存时就学到了这道难事,想知道为什么我自己的位置在地图上跳了起来

相关问题