I wrote this code using the Haversine formular for distance calculation:
private Double calculateDistance(double lat1, final double lon1, double lat2, final double lon2) {
final int R = 6371; // earth radius in km
final double dLat = Math.toRadians(lat2-lat1);
final double dLon = Math.toRadians(lon2-lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
Keep in mind, this is a straight-line (airline) distance of course, not a route. If you need a road-distance it's a whole different story, and you'll need some kind of routing-server. You could e.g. use Google Maps APIs, have a look at the Distance Matrix API or the Directions API.