Java tutorial
//package com.java2s; import android.location.Location; import java.util.ArrayList; public class Main { private static final float MILES_TO_METER_CONVERSION = (float) 1609.344; /** * Given a list of coordinates, calculate the total distance travelled. * * @param gpsCoordArray ArrayList of Location objects. Users route * @return Integer total distance in meters. */ public static int calculateTotalDistance(ArrayList<Location> gpsCoordArray) { int totalDistance = 0; for (int i = 1; i < gpsCoordArray.size(); i++) { totalDistance += getDistance(gpsCoordArray.get(i - 1), gpsCoordArray.get(i)); } return totalDistance; } /** * Returns the distance between two given locations in meters. * * @param loc1 First location object * @param loc2 Second location object * @return distance between Loc1 & Loc2 in meters. */ public static float getDistance(Location loc1, Location loc2) { double lat1 = loc1.getLatitude(); double lng1 = loc1.getLongitude(); double lat2 = loc2.getLatitude(); double lng2 = loc2.getLongitude(); double earthRad = 6371; //kilometers double dLatitude = Math.toRadians(lat2 - lat1); double dLongitude = Math.toRadians(lng2 - lng1); double a = Math.sin(dLatitude / 2) * Math.sin(dLatitude / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLongitude / 2) * Math.sin(dLongitude / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); float dist = (float) (earthRad * c); dist = dist * MILES_TO_METER_CONVERSION; return dist; } }