0

I'm trying to get the distance in meters from the past startposition/location to the moving present currentposition/location.

I do have the currentposition which works fine. But I can't get a startposition which should be fixed and saved while I start the app with the start button.

In fact, it is the same LatLng for startposition/location and currentposition/location.

I've tried to set a marker at the beginning. But i dont know how I can use it instead of the startposition to measure the distance.

 mLocationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
    super.onLocationResult(locationResult);

   if (flag == 1) {
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(
    new LatLng(locationResult.getLastLocation().
    getLatitude()
    , locationResult.getLastLocation().
     getLongitude())));
     mGoogleMap.moveCamera(CameraUpdateFactory.
    newLatLngZoom(new LatLng(locationResult.
    getLastLocation().getLatitude()
   , locationResult.getLastLocation().
    getLongitude()), 19));
   mGpsCurrentPosition = new LatLng(locationResult.
   getLastLocation().getLatitude()
   , locationResult.getLastLocation().
   getLongitude());
 mGpsStartPosition = new LatLng(locationResult.
  getLastLocation().getLatitude()
 , locationResult.getLastLocation().
   getLongitude());
    }
   updateGps();
   }
private void getLaufDistanz() {
    Location laufStartPunkt = new Location("Start");
    laufStartPunkt.setLatitude(mGpsStartPosition.latitude);
    laufStartPunkt.setLongitude(mGpsCurrentPosition.longitude);
    Location laufEndPunkt = new Location("Ende");
    laufEndPunkt.setLatitude(mGpsCurrentPosition.latitude);
    laufEndPunkt.setLongitude(mGpsCurrentPosition.longitude);
    float laufDistanzInMeter = laufStartPunkt.distanceTo
            (laufEndPunkt)/1000;
    mTvMinProKm.setText("" + String.valueOf(laufDistanzInMeter));
}

I need a way to set the startposition with LatLng to measure the distance in meters.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
elpaso
  • 11
  • 1
  • 5

1 Answers1

0

Try the Haversine Formula (see the link below). It will give you the great circle distance between two points assuming a spherical earth.

https://en.wikipedia.org/wiki/Haversine_formula

hermit
  • 56
  • 6