0

I want to calculate the distance between my location and another location and write it in a TextView at intervals of time.

I tried this:

private LocationListener mLocationListener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG_LOG, "In onLocationChanged(...)");
        if(!mLatLngGpxList.isEmpty()) {
            Location loc = new Location("");
            loc.setLatitude(mLatLngGpxList.get(0).getLatitude());
            loc.setLongitude(mLatLngGpxList.get(0).getLongitude());
            float distance = mCurrentLocation.distanceTo(loc)/1000;
            distanceTextView.setText("Distance to Track: " + distance + " Km");
        }
    }
};

(I have this too..)

final LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(UPDATE_INTERVAL);
        locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        mLocationClient.requestLocationUpdates(locationRequest, mLocationListener);

The distance chages only when the application is restarted and not in real time.

Maybe i should use a handler?

Loris
  • 454
  • 7
  • 19
  • this answer will be helpful for your question:http://stackoverflow.com/questions/18310126/get-the-distance-between-two-locations-in-android – praveen Jan 03 '14 at 10:50
  • The problem is not how to calculate the distance, but how to update the TextView in real time. I will try with a handler, called in onLocationChanged – Loris Jan 03 '14 at 11:04
  • You don't need a `Handler` for this as long as you've called `requestLocationUpdates()` on the main (UI) thread. Is `onLocationChanged()` getting called? Check your logcat. – David Wasser Jan 03 '14 at 11:27
  • Yes it's called. If i move in my house the distance don't change, but if i turn the phone it change. – Loris Jan 03 '14 at 11:47
  • I implemented the same thing with a handler and i obtain the same behavior. I want to cry. – Loris Jan 03 '14 at 12:08
  • Ok, i'm idiot. I forgot to call this istruction: mCurrentLocation = mLocationClient.getLastLocation(); in onLocationChanged... T_T – Loris Jan 03 '14 at 12:16

0 Answers0