DISTANCIA ENTRE DOS PUNTOS GEO-REFERENCIADOS
public static double distanciaCoord(double lat1, double lng1, double lat2, double lng2) {
//double radioTierra = 3958.75;//en millas
//double radioTierra = 6371;//en kilómetros
double radioTierra = 6371000;//en metros
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double va1 = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double va2 = 2 * Math.atan2(Math.sqrt(va1), Math.sqrt(1 - va1));
double distancia = radioTierra * va2;
return round(distancia, 8);
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public static double distanciaCoord(double lat1, double lng1, double lat2, double lng2) {
//double radioTierra = 3958.75;//en millas
//double radioTierra = 6371;//en kilómetros
double radioTierra = 6371000;//en metros
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double va1 = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double va2 = 2 * Math.atan2(Math.sqrt(va1), Math.sqrt(1 - va1));
double distancia = radioTierra * va2;
return round(distancia, 8);
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
Comentarios
Publicar un comentario