delight-im / Android-SimpleLocation

Utility class for easy access to the device location on Android
Apache License 2.0
200 stars 75 forks source link

Latitude longitude are 0 after turning on the location services #1

Closed rajeshgoswami2025 closed 9 years ago

rajeshgoswami2025 commented 9 years ago

I am using this library to get the location. But when GPS is not On and we turn on it by going to settings which opens from location.openSettings() method. In this case the location is coming as 0. How to get correct location?

ocram commented 9 years ago

Thanks for your feedback!

  1. Do you have <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in the manifest? Probably yes, right?
  2. Do you check availability with location.hasLocationEnabled()?

If you just enabled GPS, the location may not be available yet. So what happens if you try later again? If GPS has been enabled for a while, the next time you try, a location should be available.

Does this work?

rajeshgoswami2025 commented 9 years ago

Thanks for the quick reply, yes i have this permission in my manifest file. GPS is taking 30-60 seconds to getting enable,location.hasLocationEnabled() will give false till the processing. I am finding as much as fast response from GPS after enabling it,want to do GPS processing time less. Is it possible to get less time to enable and give me Lat,Long? I really need this.

ocram commented 9 years ago

In general, GPS just takes some time to get the first position or "fix". But there may still be some things you could do.

You may call location.beginUpdates(); as early as possible. Maybe in onCreate(...) and location.endUpdates(); in onDestroy().

Furthermore, you should explicitly set the mode to GPS (not network location) by using the constructor SimpleLocation(final Context context, final boolean requireFine) with the second parameter set to true.

By the way, if you got a position previously, the library returns a cached position before the first GPS update is received. So you should not get (0, 0) anymore in those cases.

The third parameter (passive mode or not) should probably be false for your use case (which is the default). But you may set the fourth parameter (the interval in milliseconds) to a shorter period of time (e.g. 15000 which is 15 seconds).

rajeshgoswami2025 commented 9 years ago

if (location.hasLocationEnabled()) { // ask the device to update the location data location.beginUpdates(); //...

        // get the location from the device (alternative A)
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        // get the location from the device (alternative B)
      //  SimpleLocation.Point coords = location.getPosition();
        Toast.makeText(
                getApplicationContext(),
                "Mobile Location: \nLatitude: " + latitude
                        + "\nLongitude: " + longitude,
                Toast.LENGTH_LONG).show();

        Log.i("simple location", "Mobile Location: \nLatitude: " + latitude
                + "\nLongitude: " + longitude);
        //...
        // ask the device to stop location updates to save battery
       // location.endUpdates();
    }
    else {
        Log.i("simple location"," gps disabled ");            // ask the user to enable location access
        SimpleLocation.openSettings(this);
    }

Sir,above code is which i placed at my onCreate() ,if gps is not enabled ,app is redirecting to location setting it is good ,but after enable the gps ,when come to my app ,i am getting (0,0) again ,i am continuously checking location.hasLocationEnabled() at my button event ,but nothing new happens ,,still 0,0...Please help

ocram commented 9 years ago

Thanks! We should have checked your code right from the beginning :) There were two issues:

  1. There was a bug in this library. It should have been fixed now. Thanks for pointing this out!
  2. The usage of lifecycle methods in your example was wrong. If you put all this in onCreate(...), this does not work. The reason is that, when you are redirected to the device's settings, Android won't call onCreate(...) again after you come back to your app.

So in order to fix your issue, please get the updated JAR for this library and refer to the updated code example. When you did this, please report if it worked :) Thanks!

Sidhartha03 commented 6 years ago

@ocram I have also tried the sample code link which you have provided. While opening the app for the second time is my location is off its navigating to the settings page to turn on the location, then I turn on the location and trying to retrieve the latitude and longitude on button click but getting 0.0 both latitude and longitude.How to fix this ?

ocram commented 6 years ago

@Sidhartha03 Several ways of how this may be fixed have been suggested above. Apart from that, there is a number of other issues here which you could check and the README contains detailed descriptions. Did you try all that?

shrutika229 commented 6 years ago

I have just enabled GPS and didn't get the lat long in the Oreo Device in OnePlus with oxygen version 5.1.2 in the frist glance . I have given all the permission in all other devices it is working properly only the oreo device has a problem ? Can yo tell me any solution for it ?

shubhamk1997 commented 6 years ago

@shrutika229 I am getting wrong latitude and longitude points in oneplus 2 but getting the right one in my other phone,have you sorted this out ?

ocram commented 6 years ago

@shubhamk1997 Wrong (non-zero) latitude and longitude values are something that is definitely caused by the device and OS and thus not this library. But perhaps you just need to decrease the duration between location updates. You should look at the options that the parameters of the constructor offer.