FusedLocationProviderClient иногда возвращает одно и то же местоположение - PullRequest
0 голосов
/ 11 февраля 2019

Я работаю над приложением, которое отслеживает местоположение пользователя в реальном времени, и я использую FusedLocationProviderClient для получения текущего местоположения, но иногда оно возвращает то же местоположение, т. Е. Приложение получает местоположение при открытии приложения, после перемещения в другое место, скажем, другая деревнякогда приложение пытается получить новое местоположение, оно снова получает предыдущее местоположение ...

Пожалуйста, помогите

Мой код выглядит следующим образом.

mFusedLocationClient = 
LocationServices.getFusedLocationProviderClient(this);
locationRequest = LocationRequest.create();
locationRequest.setInterval(MIN_UPDATE_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

@Override
protected void onResume() {
    super.onResume();
    try {
        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int result = googleApiAvailability.isGooglePlayServicesAvailable(this);

        if (result != ConnectionResult.SUCCESS && result != ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
            Toast.makeText(this, "Are you running in Emulator ? try a real device.", Toast.LENGTH_SHORT).show();
        }
        callCurrentLocation(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void callCurrentLocation(final int type) {
    try {
        if (
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                        ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                ) {
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            requestPermissions(REQUEST_PERMISSIONS_CURRENT_LOCATION_REQUEST_CODE);
            return;
        }

        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {

                currentLocation = (Location) locationResult.getLastLocation();
                if (currentLocation != null) {
                    latitude = currentLocation.getLatitude();
                    longitude = currentLocation.getLongitude();
                    currentAccuracy = currentLocation.getAccuracy();

                }
                String result = "Current Location Latitude is " +
                        currentLocation.getLatitude() + "\n" +
                        "Current location Longitude is " + currentLocation.getLongitude() + "  \n Accuracy is " + currentLocation.getAccuracy();


                    Toast.makeText(act, "current Location : " + result, Toast.LENGTH_SHORT).show();
                                  }
        };

        mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...