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

Lat : 0, Lon : 0 #27

Closed SezerFidanci closed 1 year ago

SezerFidanci commented 6 years ago

Hi, I followed the instructions in the readme file. But result Lat: 0.0, Lon: 0.0

`package app.meysta;

import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.TextView;

import im.delight.android.location.SimpleLocation;

public class Splash extends Activity {

private SimpleLocation location;
 double latitude =1;
 double longitude = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_splash);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);

    location = new SimpleLocation(Splash.this, true, false, 5 * 1000, true);

    // if we can't access the location yet
    if (!location.hasLocationEnabled()) {
        // ask the user to enable location access
        SimpleLocation.openSettings(Splash.this);
    }

    TextView txt = (TextView)findViewById(R.id.txtt);

    txt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             latitude = location.getLatitude();
             longitude = location.getLongitude();

            Log.i("Lokasyon", String.valueOf(latitude)+","+longitude);
            location.beginUpdates();
        }
    });

    location.setListener(new SimpleLocation.Listener() {

        public void onPositionChanged() {
            latitude = location.getLatitude();
            longitude = location.getLongitude();

        }

    });

}

@Override
protected void onResume() {
    super.onResume();

    // make the device update its location
    location.beginUpdates();
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    // ...
}

@Override
protected void onPause() {
    // stop location updates (saves battery)
    location.endUpdates();

    // ...

    super.onPause();
}

} `

ocram commented 6 years ago

Well, your implementation looks good. It seems you have indeed read the documentation well and checked other issues for a solution. Thank you!

  1. Did you try this on a real device or on the emulator? If it’s the latter, try a real device instead.

  2. In the onClick callback of your View.OnClickListener, the call

    location.beginUpdates();

    is definitely redundant. You can safely remove that.

  3. Copy your Log.i call and paste it at the end of the onPositionChanged callback of SimpleLocation.Listener. Of course, it might make sense to change the message string as well. Otherwise, you don’t see when new location data becomes available.

  4. Try changing the second argument of the constructor from true to false to switch between GPS location and network location.

Does that help?

In general, this is unfortunately quite a common problem, but still one that can easily be solved, I’d say:

spidgorny commented 6 years ago

If you target a newer Android platform, you should ask a user for access to their location explicitly. https://developer.android.com/training/permissions/requesting

anmol-Git commented 3 years ago

when i am overriding the onResume method my UI get stuck and black screen occur any suggestions