3

I would like to get the latitude and longitude from my current location in my app. I am testing on a Galaxy Wi-Fi 5.0. I only need the numbers in double format as I am gonna use it in a google-maps-url. The problem is lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) returns null and i get a nullpointerexception.

private void getLatitudeLongitude()
{
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location != null) {
            showMyAddress(location);
        }

        final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                showMyAddress(location);
            }

            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub

            }

            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub

            }

            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }
        };

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);


        Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
        try {
            System.out.println(myLocation.getFromLocation(latitude, longitude, 1).toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

private void showMyAddress(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    try {
        System.out.println( myLocation.getFromLocation(latitude, longitude, 1).toString());

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
  • 3
    have you added the permissions to the manifest? – zozelfelfo Nov 19 '14 at 11:54
  • Check : http://stackoverflow.com/questions/19874677/finding-location-on-google-map/19874802#19874802 – Haresh Chhelana Nov 19 '14 at 11:59
  • I have added this permission @zozelfelfo – themanwithballs Nov 19 '14 at 12:03
  • 1
    You are using only GPS, so make sure you aren't sitting in a closed room while testing it... ensure line-of-sight for the satellite, do you get the same when you go out into the open or stand at a window – Pararth Nov 19 '14 at 12:06
  • 2
    refer [this][1] Also see [this][2] and [this][3] too. [1]: http://stackoverflow.com/questions/17591147/how-to-get-current-location-in-android [2]: http://stackoverflow.com/questions/22524844/how-to-get-location-of-android-device-by-its-android-id [3]: http://stackoverflow.com/questions/19874677/finding-location-on-google-map/19874802#19874802 –  Nov 19 '14 at 12:07
  • Guys, the problem is that "location" is null. Please see edited question right before the code-snip. How can I fix this? – themanwithballs Nov 19 '14 at 13:28

0 Answers0