【问题标题】:Both LocationListener and Fusedlocationproviderclient are returning null valuesLocationListener 和 Fusedlocationproviderclient 都返回空值
【发布时间】:2021-08-10 08:57:54
【问题描述】:

我知道这个问题已被多次询问和回答,但没有一个对我有用,我的目标是获取用户当前位置,然后在该位置显示 GoogleMaps 标记,但我完全无法做到这一点,因为 LocationListener和我尝试的 Fusedlocationproviderclient 正在返回 null 值,这里有一些示例代码:

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,  mLocationListener);

...
private final android.location.LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(@NonNull Location location) {
        app.l("Fetched");
        currentLocation = location ;
// Null

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};

------------另一种方式----

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        fetchLocation();

private void fetchLocation() {
    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(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        } else {

            LocationRequest mLocationRequest = LocationRequest.create();
            mLocationRequest.setInterval(60000);
            mLocationRequest.setFastestInterval(5000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            LocationCallback mLocationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    if (locationResult == null) {
                        return;
                    }
                    for (Location location : locationResult.getLocations()) {
                        if (location != null) {
                            app.l("sth1");
                        }
                    }
                }
            };

            LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, mLocationCallback, null);
            LocationServices.getFusedLocationProviderClient(this).getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location == null){
                        app.l("it's null");
                    }
                }
            });

        }

    }

P.S:我已经提供并检查了所有必需的权限

【问题讨论】:

    标签: java android location


    【解决方案1】:

    这是一个简单的解决方案:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    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.
                    return;
                }
                currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (currentLocation == null) {
                }
                if (currentLocation != null) {
    
                    }
        
                }
    

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 2019-08-19
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2019-12-23
      • 2021-04-12
      • 1970-01-01
      相关资源
      最近更新 更多