1

I enabled the GPS in my device but I am not getting the loaction:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(location!=null)
    {
        showCurrentAddress(location);
    }
}

But here I am gettin location is null and I added the following permissions to my code:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

Could you please help?

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 2
    Try this [link][1] [1]: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655 For Discuss Get GPs Location – Dinesh Mar 26 '12 at 08:15
  • Try this [link][1] [1]: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655 – Dinesh Mar 26 '12 at 09:08

2 Answers2

1

lm.getLastKnownLocation can return null because that is a getter for the last known location.

Try using lm.requestLocationUpdates(LocationListener).

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
lulumeya
  • 1,619
  • 11
  • 14
0

Hi check this tutorial for GPS location , i am also using this code for getting latitude and longitudeUsing GPS to get current location – Android tutorial check this on your device not on emulator first time just wait for few seconds..

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166