public final double getDistanceToKm ( Position position ) { double lat1 = toRadians ( lat ) ; double lat2 = toRadians ( position . lat ) ; double lon1 = toRadians ( lon ) ; double lon2 = toRadians ( position . lon ) ; double deltaLon = lon2 - lon1 ; double cosLat2 = cos ( lat2 ) ; double cosLat1 = cos ( lat1 ) ; double sinLat1 = sin ( lat1 ) ; double sinLat2 = sin ( lat2 ) ; double cosDeltaLon = cos ( deltaLon ) ; double top = sqrt ( sqr ( cosLat2 * sin ( deltaLon ) ) + sqr ( cosLat1 * sinLat2 - sinLat1 * cosLat2 * cosDeltaLon ) ) ; double bottom = sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosDeltaLon ; double distance = radiusEarthKm * atan2 ( top , bottom ) ; return abs ( distance ) ; }
returns distance between two WGS84 positions according to Vincenty s formula from Wikipedia