Ryan D
Ryan D

Reputation: 751

Update gps location in android studio every couple seconds

I am new to android development but am taking a shot at making myself a golf rangefinder.. I have this activity -

public class hole_1 extends Activity implements View.OnClickListener {

Button nextBtn;
GPSTracker gps;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hole_1);
    gps = new GPSTracker(hole_1.this);

    // Get Variable From Home Activity
    Bundle extras = getIntent().getExtras();
    String course = null;
    if (extras != null) {
        course = extras.getString("Course");

    }



    //Set Next Hole Button
    nextBtn = (Button) findViewById(R.id.nextButton);
    nextBtn.setOnClickListener(this);

    gps = new GPSTracker(hole_1.this);

    if (gps.canGetLocation()) {

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        double lat2 = 39.765718;
        double lon2 = -121.860080;


        Location loc1 = new Location("");
        loc1.setLatitude(latitude);
        loc1.setLongitude(longitude);

        Location loc2 = new Location("");
        loc2.setLatitude(lat2);
        loc2.setLongitude(lon2);

        float distanceInMeters = loc1.distanceTo(loc2);

        int myDist = (int) (distanceInMeters * 1.0936);

        TextView latView = (TextView) findViewById(R.id.yardage);
        latView.setText(String.valueOf(myDist));




    }else{
        gps.showSettingsAlert();
    }

}





@Override
public void onClick(View v) {

    Intent myIntent = new Intent(this, end.class);
    myIntent.putExtra("Hole",1);
    startActivity(myIntent);

}
}

What I would like to do is update the myDist variable which is the distance between my current coordinates and the fixed coordinates (lat2, lon2). I have done some research and found asyncTask, threading and setting a timer but cannot figure out the best method for this application.. The app works great as is but I have to refresh the page to get updated distances and would like it to just update itself every few seconds.. What should I do?

Thanks!

Upvotes: 1

Views: 1358

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93561

1)You can't update every couple of seconds. GPS only updates every 30s to a minute.

2)You wouldn't use an async task here or any oter form of threading, GPS works on a callback system. You request updates and it will call you back whenever an update is available.

3)DO NOT USE the GpsTracker LIBRARY EVER. It's broken. Badly. See the full writeup I have on why its broken at http://gabesechansoftware.com/location-tracking/

Upvotes: 2

Related Questions