简体   繁体   中英

Location returning null on some devices

I have been working in an Application where i need to fetch the current location of users. For the same, I added the COARSE_LOCATION AND FINE_LOCATION permissions in Manifest file.

I also added the Runtime permission to the App and i checked that the permissions are enabled in the App Info.

I have been using the below code to fetch the user's location.

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(new Criteria(), true);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        } else {
            Location locations = locationManager.getLastKnownLocation(provider);
            List<String> providerList = locationManager.getAllProviders();
            if (null != locations && null != providerList && providerList.size() > 0) {
                double longitude = locations.getLongitude();
                double latitude = locations.getLatitude();
                Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                try {
                    List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
                    if (null != listAddresses && listAddresses.size() > 0) {
                        cityname = listAddresses.get(0).getLocality();
                        countryname = listAddresses.get(0).getCountryName();
                        regionname = listAddresses.get(0).getSubLocality();

                        System.out.println("Location updates = " + cityname + regionname + countryname);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

This piece of code works good on some devices (REDMI NOTE 3, REDMI NOTE 4, MOTO E) and in some devices it returns null(SAMSUNG J7, MOTO G).

Did i went wrong anywhere? Any advise can be really great full for my project and my learning.

I had the same problem. I put locationManager.getLastKnownLocation(provider); code inside loop until I get current location latitude and longitude. See below code :

@Override
    public void onMapReady(GoogleMap googleMap) {
  mMap = googleMap;

                 Location location = null;
                 do {
                     location = getLastKnownLocation();
                  }
                  while (location == null);

                   currentLatitude = location.getLatitude();
                  currentLongitude = location.getLongitude();
  } 




private Location getLastKnownLocation() {
        List<String> providers = locationManager.getProviders(true);

        Location bestLocation = null;
        for (String provider : providers) {
            Location l = null;
            try {
                l = locationManager.getLastKnownLocation(provider);
            } catch (SecurityException e) {
                e.printStackTrace();
            }
            if (l == null) {
                continue;
            }
            if (bestLocation == null
                    || l.getAccuracy() < bestLocation.getAccuracy()) {
                bestLocation = l;
            }
        }
        if (bestLocation == null) {
            return null;
        }
        return bestLocation;
    }

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location == null) {

            do{
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }while (location == null);

            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
            Log.d("locationLinster",message);
}else{
      String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
      Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
            Log.d("locationLinster",message);
}

This really help to kick start the Provider... it takes time if our having this problem for the 1st time. But when it kicks in. it works like a dream.

Thanks @RG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM