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

I've just copy+paste the code but returns 0.0 #24

Closed apimyself closed 6 years ago

apimyself commented 6 years ago

Sorry for disturbing .. I am trying the code on emulator. I've just copy+paste the code from Readme file and it returns 0.0 . Here is my code ;

public class MainActivity extends AppCompatActivity {

private SimpleLocation location;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextEdit tw1 = (TextEdit) findViewById(R.id.textView);
    final TextEdit tw2 = (TextEdit) findViewById(R.id.textView2);
    // 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(this);
    }

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final double latitude = location.getLatitude();
            final double longitude = location.getLongitude();

            tw1.setText("Lat: "+latitude);
            tw2.setText("Lng: "+longitude);

            // TODO
        }

    });
}

@Override
protected void onResume() {
    super.onResume();
    // make the device update its location
    location.beginUpdates();
    // ...
}

@Override
protected void onPause() {
    // stop location updates (saves battery)
    location.endUpdates();
    // ...
    super.onPause();
}
ocram commented 6 years ago

Please see the various other (closed) issues related to coordinates of 0.0 and possible solutions. Thanks!