位置纬度,经度未在出行模式下更新

问题描述

假设我正在从一个城市旅行到另一个城市。当用户按下以查看其位置应用程序以显示其先前位置或错误位置时,在公共汽车内。但是几分钟后,如果用户呆在那里,则应用会显示正确的位置。

public void CurrentLocation() {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
            }
        };

        LocationRequest mLocationRequest = new LocationRequest();
        //  mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        //mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        try {
            mFusedLocationClient
                    .getLastLocation()
                    .addOnCompleteListener(new OnCompleteListener<Location>() {
                        @Override
                        public void onComplete(@NonNull Task<Location> task) {
                            if (task.isSuccessful() && task.getResult() != null) {
                                mLocation = task.getResult();
                                Log.d(TAG,"Location : " + mLocation);

                               

                                try {
                                    Geocoder geocoder = new Geocoder(getActivity(),Locale.getDefault());

                                    List<Address> addresses = null;
                                    try {
                                        addresses = geocoder.getFromLocation(mLocation.getLatitude(),mLocation.getLongitude(),1);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    String address = addresses.get(0).getAddressLine(0);
                                    String city = addresses.get(0).getLocality();
                                    String state = addresses.get(0).getAdminArea();
                                    String zip = addresses.get(0).getPostalCode();
                                    String country = addresses.get(0).getCountryName();
                                    String locality = addresses.get(0).getLocality();
                                    String SubLocality = addresses.get(0).getSubLocality();
                                    String SubAdminArea = addresses.get(0).getSubAdminArea();
                                   }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }


                            } else {
                                Log.w(TAG,"Failed to get location.");
                            }
                        }
                    });
        } catch (SecurityException unlikely) {
            Log.e(TAG,"Lost location permission." + unlikely);
        }

        try {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest,null);
        } catch (SecurityException unlikely) {
            Log.e(TAG,"Lost location permission. Could not request updates. " + unlikely);
        }
    }

我也尝试更新位置,但不起作用

  protected void onResume() {
        super.onResume();

        if (checkLocationPermission()) {
            startLocationUpdates();
        }

    }

    private void startLocationUpdates() {
        CurrentLocation("");
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          
            return;
        }
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,mLocationCallback,Looper.getMainLooper());
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)