Bhavin Jadav
Bhavin Jadav

Reputation: 274

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?

Upvotes: 1

Views: 562

Answers (2)

Shivam Oberoi
Shivam Oberoi

Reputation: 1445

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

Upvotes: 1

Tomin B Azhakathu
Tomin B Azhakathu

Reputation: 2686

Go to maps default application and allow user to allow latitude and longitude option.In our application When the GPS signal blinks and when it stops we will get the latitude and longitude. This Worked for me

Also Check

  1. Open Google Maps app and click button on map to get current location.

  2. When you allow Google map permission, then only other apps permission will work.

  3. Now go to your app and try to get current location and it will work.

Try These Steps and Check whether you are getting your Location

Edit 1

Do you check availability with location.hasLocationEnabled().

This will help you to check whether location service is available or not. and turn on GPS. This will solve the problem up to an Extent without opening Google Map

Upvotes: 0

Related Questions