其他分享
首页 > 其他分享> > android-FusedLocationProviderClient有时会提供不同的位置

android-FusedLocationProviderClient有时会提供不同的位置

作者:互联网

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

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

解决方法:

FusedLocationProviderClient的意思是将gps和google-translate-received-wlan-cellphone-tower-signals-to-lat-lon结合起来.

谷歌翻译收到的将wlan-cell-tower-signals-to-lat-lon转换为不精确的试探法,除其他因素外,还取决于您方便使用的手机塔与之连接的位置(以及google在每个塔上有lat / lon的位置) )以及最强的3个手机发射塔的强度.

如果您的手机改变了,那就是手机-塔式连接,那么对于googl-s算法,您的手机位置已经改变.

如果您的手机可以接收3个以上的手机发射塔,则最强的3个手机发射塔也将根据每个手机发射塔的通信量而变化.

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

当我第一次使用全新的Cellpone进行地理缓存时,我学到了这一困难的方法,并且想知道为什么我自己的位置在地图上跳跃

标签:android-location,android-fusedlocation,android
来源: https://codeday.me/bug/20191108/2007501.html