Thomas_isha
Thomas_isha

Reputation: 65

android location getting null need solution

String logloc ="";
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null) {
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    logloc = String.valueOf(latitude)+","+String.valueOf(longitude);
    notfi(logloc); 
}

in this above code my location is always getting null even my gps device is on and both outside the building and inside the building , i'm getting null value only , kindly help me run this code

Upvotes: 4

Views: 10490

Answers (2)

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

try like this your Activity must be like this

Activity extends MapActivity implements LocationListener

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
 String provider = locationManager.getBestProvider(criteria, false);
 Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        lat = (double) (location.getLatitude());
        lng = (double) (location.getLongitude());

        Log.i(TAG, "Lattitude:" + lat);
        Log.i(TAG, "Longitude:" + lng);

    }

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

Just becuase your device has not the last known location, thats why. First make the code for onLocationChanged(). It will get the current location and save it as lastKnownLocation. Then the method will work.

You can take help for current location here

How do I get the current GPS location programmatically in Android?

Upvotes: 1

Related Questions