OBTENER DISTANCIA ENTRE DOS PUNTOS DE LATITUD Y LONGITUD
package com.prueba;
public class GeoReferencia {
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 distancia;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//1 => LIMA
//2 => LONGITUD
double lat1 = -12.083601230478248;
double lon1 = -77.02730853231041;
double lat2 = -11.062846666666667;
double lon2 = -77.552605;
double respuesta = distanciaCoord(lat1, lon1, lat2, lon2);
System.out.println("Respuesta : " + String.valueOf(respuesta) + " m");
}
}
package com.prueba;
public class GeoReferencia {
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 distancia;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//1 => LIMA
//2 => LONGITUD
double lat1 = -12.083601230478248;
double lon1 = -77.02730853231041;
double lat2 = -11.062846666666667;
double lon2 = -77.552605;
double respuesta = distanciaCoord(lat1, lon1, lat2, lon2);
System.out.println("Respuesta : " + String.valueOf(respuesta) + " m");
}
}
Comentarios
Publicar un comentario