Search code examples
javaandroidlocationmanager

Unable to get Latitude and Longitude android


I am trying to get Latitude and Longitude using given below code but every time i am getting 0.00 and 0.00 lat and long as result.

locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000L, 1, listener);
Log.d("Network", "Network");
if (locationManager != null) {
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null) {
        Log.e("Google latitude 111", location.getLatitude() + "");
        Log.e("Google longitude 111", location.getLongitude() + "");
        if (ServiceManager.lat.equals("") || ServiceManager.lng.equals("")) {
            ServiceManager.lat = String.valueOf(location.getLatitude());
            ServiceManager.lng = String.valueOf(location.getLongitude());
            MainActivity.handler.sendEmptyMessage(Response.GOT_LOCATION);
        }
    }
}

I want to get actual latitude and longitude. I have taken and allowed all require permissions in device (FINE_LOCATION, COARSE_LOCATION and INTERNET).My target SDK version is 25. Is that any update in Androids Location Manager Policy for latest SDKs?


Solution

  • If you are working with android version 23 or greater i.e marshmallow or greater you need to add runtime permissions i.e access fine location and access coarse location then only you will get result else if you are working with version less than 23 you will get latitude and longtitude and dont forget to add permissions in manifest.

    Link-How to request Location Permission on Android 6