Closed rajeshgoswami2025 closed 9 years ago
Thanks for your feedback!
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
in the manifest? Probably yes, right?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?
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.
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).
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
Thanks! We should have checked your code right from the beginning :) There were two issues:
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!
@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 ?
@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?
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 ?
@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 ?
@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.
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?