Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting latitude and longitude 0.0 #22

Open
Sidhartha03 opened this issue Nov 20, 2017 · 3 comments
Open

Getting latitude and longitude 0.0 #22

Sidhartha03 opened this issue Nov 20, 2017 · 3 comments
Labels

Comments

@Sidhartha03
Copy link

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.

@ocram ocram added the question label Nov 21, 2017
@ocram
Copy link
Contributor

ocram commented Nov 21, 2017

  1. Did you follow all steps from the README?
  2. Can you log something in the onPositionChanged callback?
  3. Can you show your call to the SimpleLocation constructor?

@Sidhartha03
Copy link
Author

Sidhartha03 commented Nov 21, 2017

  1. 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();
    }
    }

@ocram
Copy link
Contributor

ocram commented Nov 24, 2017

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.

@ocram ocram mentioned this issue Apr 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants