Getting latitude and longitude 0.0
If the location is previously on while running the application first time, it's picking the latitude and longitude.Then if I close the app and turn off the location, while opening the app for the second time its navigating to settings to turn on location but after location is on it is unable to pick the latitude and longitude. I am getting 0.0 for both.
- Did you follow all steps from the README?
- Can you log something in the
onPositionChangedcallback? - Can you show your call to the
SimpleLocationconstructor?
-
yes I have followed the readme.Please go through the following code. my activity code :public class MainActivity extends AppCompatActivity {
Button btn_location; private SimpleLocation location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
btn_location = (Button) findViewById(R.id.btn_location); // construct a new instance of SimpleLocation location = new SimpleLocation(this); // if we can't access the location yet if (!location.hasLocationEnabled()) { // ask the user to enable location access SimpleLocation.openSettings(MainActivity.this); } btn_location.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!location.hasLocationEnabled()) { // ask the user to enable location access SimpleLocation.openSettings(MainActivity.this); } final double latitude = location.getLatitude(); final double longitude = location.getLongitude(); if (latitude != 0.0 && longitude != 0.0) { Log.d("LOCATION", "LATITUDE :" + latitude); Log.d("LOCATION", "LONGITUDE :" + longitude); Toast.makeText(MainActivity.this, "Latitude is :" + latitude + " longitude is :" + longitude, Toast.LENGTH_SHORT).show(); }else{ Log.d("LOCATION", "LATITUDE :" + latitude); Log.d("LOCATION", "LONGITUDE :" + longitude); location.beginUpdates(); } } });} @Override protected void onResume() { super.onResume(); if (!location.hasLocationEnabled()) { // ask the user to enable location access SimpleLocation.openSettings(this); }
// make the device update its location location.beginUpdates();} @Override protected void onPause() { // stop location updates (saves battery) location.endUpdates(); super.onPause(); } }
First, you should be able to safely remove the if (!location.hasLocationEnabled()) branch and its contents from onResume because you have it in onCreate already.
Second, the additional call to location.beginUpdates() in the OnClickListener in onCreate is probably superfluous.
Otherwise, this looks good. But why don’t you implement the onPositionChanged callback as shown in the README and try to log something there. That way, you would see if and when location updates are available.
Last but not least, you should try the extended arguments of the constructor, again, as shown in the README. For example, try replacing
location = new SimpleLocation(this);
with
location = new SimpleLocation(this, true, false, 5 * 1000, true);
and see if that helps. It should attempt to update the location every 5 seconds.