0

I'm trying to get my current location in my app using the code below:

    List<Address> adds = null;
    double lat;
    double lng;
    private Geocoder geocoder;
    String bestProvider;

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    bestProvider = lm.getBestProvider(criteria, false);
    Location location = lm.getLastKnownLocation(bestProvider);


    if (location == null)
    {
         Toast.makeText(this,"Location Not found",Toast.LENGTH_LONG).show();
    }
    else
    {
        geocoder = new Geocoder(this);
        try 
        {
            users = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            lat=(double)users.get(0).getLatitude();
            lng=(double)users.get(0).getLongitude();
            System.out.println(" DDD lat: " +lat+",  longitude: "+lng);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

But location keeps showing up as null, therefore the Location Not found message appears on app. Am i missing something?

Manifest permissions:

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

Any help would be appreciated.

Peter
  • 521
  • 2
  • 6
  • 20
  • 1
    http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null Use the search feature next time! – Paul Aug 20 '12 at 21:52
  • Are you trying it in a device? – Lazy Ninja Aug 21 '12 at 01:13
  • I am, it turns out that it was returning that the network was the best provider for location, but the devices setting at the time had network location services turned off, it was a weird one, but it works now. – Peter Aug 21 '12 at 08:32

0 Answers0