1

I'm doing kinda running tracking app. My app already tracks my path and distance, but I can't understand what do those numbers in distance mean. Here are the screenshots(1,2,3). I want to get my distance in kilometers in this format DISTANCE FORMAT, like on the picture(example: 1,11 KM). Please, help me. I'm new in Android Development, so could you please explain me how to do it.

Thanks beforehand.

HERE IS MY CODE

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        if(currentLocationMarker != null){
            currentLocationMarker.remove();
        }
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions marker = new MarkerOptions();
        marker.position(latLng);
        currentLocationMarker=mMap.addMarker(marker);
        polylineOptions = new PolylineOptions();
        polylineOptions.color(Color.RED);
        polylineOptions.width(5);
        arrayPoints.add(latLng);
        polylineOptions.addAll(arrayPoints);
        mMap.addPolyline(polylineOptions);
        calculateMiles();
    }
}
protected float calculateMiles() {
    float totalDistance = 0;

    for(int i = 1; i < polylineOptions.getPoints().size(); i++) {
        Location currLocation = new Location("this");
        currLocation.setLatitude(polylineOptions.getPoints().get(i).latitude);
        currLocation.setLongitude(polylineOptions.getPoints().get(i).longitude);
        Location lastLocation = new Location("this");
        currLocation.setLatitude(polylineOptions.getPoints().get(i-1).latitude);
        currLocation.setLongitude(polylineOptions.getPoints().get(i-1).longitude);
        totalDistance += lastLocation.distanceTo(currLocation);
    }


    textAutoUpdateLocation.setText("Distance = " + totalDistance);
    return totalDistance;
}
JAGUAR
  • 21
  • 8

2 Answers2

2
double i2=i/60000;  tv.setText(new DecimalFormat("##.##").format(i2));

output:

5.81
Jay Nirmal
  • 380
  • 2
  • 10
  • I don't know why, but on first onLocationChange I already have 110,87. I have added everything as you told. – JAGUAR Aug 14 '18 at 17:17
  • totalDistance += lastLocation.distanceTo(currLocation); double kms=totalDistance/60000; textAutoUpdateLocation.setText(new DecimalFormat("##.##").format(kms)); } return totalDistance; } – JAGUAR Aug 14 '18 at 17:18
  • And it counts distance even if I stay on one place. – JAGUAR Aug 14 '18 at 17:20
1

Okay, so you are getting the distance, which is in meters as per the documentation. SO you can make it into kms by diving by 1000. And can finally format it into your desired format.

You can pass in the distance you got in the below method, and try if it helps.

   String format(float distanceInMetres) {
        float kms = distanceInMetres / 1000;
        NumberFormat formatter = NumberFormat.getInstance();
        return formatter.format(kms);
   }
Anmol
  • 448
  • 2
  • 6
  • Could you tell me please where I have to add it and how it has to look like? – JAGUAR Aug 14 '18 at 16:36
  • totalDistance += lastLocation.distanceTo(currLocation); float kms = totalDistance / 1000; NumberFormat formatter = NumberFormat.getInstance(); textAutoUpdateLocation.setText("Distance="+kms); return String.valueOf(formatter.format(kms)); } return null; } – JAGUAR Aug 14 '18 at 16:36
  • textAutoUpdateLocation.setText("Distance = " + totalDistance); change this line to: textAutoUpdateLocation.setText("Distance = " + format(totalDistance)); – Anmol Aug 14 '18 at 16:37
  • But it gives me back 6648.735. Couldn't be this distance. – JAGUAR Aug 14 '18 at 16:37