1

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?

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Bhavin Jadav
  • 274
  • 1
  • 3
  • 14
  • Do u want to get current latitude and longitude? – Lokesh Desai Dec 05 '17 at 04:36
  • @BhavinJadav Have you tried it by implementing **LocationListener**. – Harshad Prajapati Dec 05 '17 at 04:39
  • Yes @LokeshDesai and i have also tried LocationListener. – Bhavin Jadav Dec 05 '17 at 04:43
  • @BhavinJadav OK. if you know about Rx than i recommend you to use [this](https://github.com/florent37/RxGps) library. Its better and more efficient and fast.if you have any doubt how to use it let me know. – Harshad Prajapati Dec 05 '17 at 04:52
  • @BhavinJadav can you check with GPS provider instead of Network provide once? – Lokesh Desai Dec 05 '17 at 05:01
  • @BhavinJadav try to run your app in lollipop version then check latitude and longitude if this works for you then add run time permissions and check location is on in your phone or not in order to run versions that is greater than lollipop hope it might help you happy coding :) – Tara Dec 05 '17 at 07:09

2 Answers2

1

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

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
0

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

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28