Reputation: 21
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;
}
Upvotes: 1
Views: 1175
Reputation: 400
double i2=i/60000; tv.setText(new DecimalFormat("##.##").format(i2));
output:
5.81
Upvotes: 2
Reputation: 568
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);
}
Upvotes: 1