Python calculates the distance example based on latitude and longitude

  • 2020-04-02 13:27:21
  • OfStack



public static double getDistance(double _lat1,double _lon1, double _lat2,double _lon2){
 double lat1 = (Math.PI/180)*_lat1;
 double lat2 = (Math.PI/180)*_lat2;

 double lon1 = (Math.PI/180)*_lon1;
 double lon2 = (Math.PI/180)*_lon2;

 //Radius of the earth
 double R = 6378.1;

 double d =  Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;

 return new BigDecimal(d).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static void main(String[] args) {
 System.out.println(getDistance(45.73990, 126.55893,45.73876, 126.55037));
}


Related articles: