FusedLocationProvider возвращает ноль - PullRequest
0 голосов
/ 28 октября 2018

Я использовал клиента провайдера fusedlocation для получения местоположения устройства. Но если настройка местоположения отключена от моего телефона, и откройте настройку местоположения с помощью locationServices. getSettingsClient - объединенный поставщик местоположения, возвращающий нуль

 final Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
        locationTask.addOnSuccessListener((Activity) context, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    lastKnownLocation = location;
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                            new LatLng(lastKnownLocation.getLatitude(),
                                    lastKnownLocation.getLongitude()), Constants.DEFAULT_ZOOM));
                    LatLng position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
                    HashMap<String, String> adressInfo = MapUtility.getAddressGeocoderObject(context, position);

                    MapUtility.moveCamera(context, map, position, Constants.DEFAULT_ZOOM,
                            adressInfo.get("addressFeatureName"), adressInfo.get("remainAdress"));


                } else {
                    Toast.makeText(context, "null", Toast.LENGTH_LONG).show();
                    //map.getUiSettings().setMyLocationButtonEnabled(false);

                }

1 Ответ

0 голосов
/ 28 октября 2018

решил мою проблему при обновлении провайдера fusedLocation

private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10*1000);
    mLocationRequest.setFastestInterval(10*1000);
    mLocationRequest.setSmallestDisplacement(10);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

private void createLocationCallback(){
    locationCallback=new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
           //code here

        }
    };

}

и если нулевое местоположение обновит провайдера fused location

 public void getDeviceLocation() {
    try {
        final Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
        locationTask.addOnSuccessListener((Activity) context, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    lastKnownLocation = location;
                    if(map==null){
                        Toast.makeText(context,"map null",Toast.LENGTH_LONG).show();
                        return;
                    }
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                            new LatLng(lastKnownLocation.getLatitude(),
                                    lastKnownLocation.getLongitude()), Constants.DEFAULT_ZOOM));
                    LatLng position = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
                    HashMap<String, String> adressInfo = MapUtility.getAddressGeocoderObject(context, position);

                    assert adressInfo != null;
                    MapUtility.moveCamera(context, map, position, Constants.DEFAULT_ZOOM,
                            adressInfo.get("addressFeatureName"), adressInfo.get("remainAdress"));

                } else {
                    createLocationRequest();
                    createLocationCallback();
                    fusedLocationProviderClient.requestLocationUpdates(mLocationRequest,locationCallback,Looper.myLooper());

                }


            }


        });

        locationTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(Constants.PlacesTag, "Current location is null. Using defaults.");
            }
        });


    } catch (SecurityException e) {
        Log.e("Exception: %s", e.getMessage());
    }

}
...