Android-SimpleLocation icon indicating copy to clipboard operation
Android-SimpleLocation copied to clipboard

Lat : 0, Lon : 0

Open SezerFidanci opened this issue 7 years ago • 3 comments

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

} `

SezerFidanci avatar Apr 26 '18 17:04 SezerFidanci

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:

  • https://github.com/delight-im/Android-SimpleLocation/issues/24
  • https://github.com/delight-im/Android-SimpleLocation/issues/22
  • https://github.com/delight-im/Android-SimpleLocation/issues/20
  • https://github.com/delight-im/Android-SimpleLocation/issues/15
  • https://github.com/delight-im/Android-SimpleLocation/issues/11
  • https://github.com/delight-im/Android-SimpleLocation/issues/9
  • https://github.com/delight-im/Android-SimpleLocation/issues/3
  • https://github.com/delight-im/Android-SimpleLocation/issues/2
  • https://github.com/delight-im/Android-SimpleLocation/issues/1

ocram avatar Apr 26 '18 20:04 ocram

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

spidgorny avatar May 28 '18 02:05 spidgorny

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

anmol-Git avatar Nov 27 '20 04:11 anmol-Git