4
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

Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
Thomas_isha
  • 65
  • 1
  • 1
  • 9

2 Answers2

1

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?

Community
  • 1
  • 1
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • thanks for your reply , what about lm ? we getting getLastKnownLocation using lm.getLastKnownLocation – Thomas_isha Aug 23 '13 at 06:54
  • your code is fine, but that time no current location you need to used locationlistner and automatically triger OnLocationChanged and get the current lat and long – Sunil Kumar Aug 23 '13 at 06:59
0

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);

    }
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98