Vlad Alexeev
Vlad Alexeev

Reputation: 2214

How else can I get my geolocation coordinates?

This is how I get my latitude and longtitude.

Criteria criteria = new Criteria();
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            String provider = locationManager.NETWORK_PROVIDER;
            Location location = locationManager.getLastKnownLocation(provider);
            if(location==null)
            {
            //do nothing    
            }else
            {
       double      longitude = location.getLongitude();
       double         latitude = location.getLatitude();
       saveCoordinates(location); // there goes another method

            }

here's list of permissions on my device everything works ok, but on other devices (like tablets) this doesn't return any coordinates.. As I understand, it asks gps and network coordinates and this code doesn't return anything. But when they use other apps like google maps , their location is determined correctly.

So, how else can I get coordinates?

Upvotes: 0

Views: 67

Answers (1)

Rohan Kandwal
Rohan Kandwal

Reputation: 9336

You will have to ask for a new location. Add the following in your else statement

LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

and this outside your function.

private class mylocationlistener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        Toast.makeText(MainActivity.this,
            location.getLatitude() + "" + location.getLongitude(),
            Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    }

Note:-

Also instead of creating a new class you can always add implements LocationListener to your activity. Then too you will get the above 4 function. Add same code to them.

Upvotes: 1

Related Questions