LorenzoBerti
LorenzoBerti

Reputation: 6974

get current location android

I have a problem with getting current location because, I have in my MainActivity the listener:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATE,
                MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                new MyLocationListener()
        );

And I have a second activity where I would, when i click on button, calculate distance from my location and gps coordinate that I will pass with distanceTo and return true if the distance is beetween 0 and 200. So I have a function in SecondActivity

private boolean checkCoordinate(String gps) {
        LocationManager lm = (LocationManager) getSystemService(App.getContext().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) {

            return false;
        }

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

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

        Location loc1 = new Location("");
        loc1.setLatitude(currentLatitude);
        loc1.setLongitude(currentLongitude);



        String[] sep = gps.split(",");
        double latitude = Double.parseDouble(sep[0]);
        double longitude = Double.parseDouble(sep[1]);



        Location loc2 = new Location("");
        loc2.setLatitude(latitude);
        loc2.setLongitude(longitude);

        float distanceInMeters = loc1.distanceTo(loc2);
        if(distanceInMeters < 200){
            return true;
        }else{
            return false;
        }
    }

But the problem is that someTimes and maybe when I don't change my location the function:

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

return null, and I don't understand how know my current location because I have already set requestLocationUpdates. so I how can know my current location?

Upvotes: 0

Views: 447

Answers (1)

Marvin W
Marvin W

Reputation: 444

getLastKnownLocation will return null if GPS based location is disabled in system or there was no GPS fix since system startup. If you want to ensure there is a location you can use the requestSingleUpdate-method, it will not return until there is a location available.

It might also be a good idea to not use the APIs based on specific provider name if it's not important to use a specific provider (usually it is not important how you got the location). The API contains methods that take a Criteria-object instead of a provider name and you can define very precisely what accuracy level etc. you want.

Upvotes: 1

Related Questions