Showing posts with label calculating distance of coordinates. Show all posts
Showing posts with label calculating distance of coordinates. Show all posts

Thursday, 16 August 2012

Getting distance of coordinates using java

Getting distance of coordinates 



Yesterday came across way of calculating the distance between coordinates(latitude & longitude) and as usual the application is to be written in java, so after my search i got this snippet i would like to share below which can be used as desired. 
public static double distFrom2(double p_dbllatitude1, double p_dbllongitude1, double p_dbllatitude2, double p_dbllongitude2) { double radianLat1 = p_dbllatitude1 * Math.PI / 180 ; double radianLat2 = p_dbllatitude2 * Math.PI / 180 ; double radiandiff1 = radianLat1 - radianLat2 ; double radiandiff2 = p_dbllongitude1 * Math.PI / 180 - p_dbllongitude2 * Math.PI / 180 ; double s = 2 * Math.asin ( Math.sqrt ( Math.pow ( Math.sin ( radiandiff1 / 2 ) , 2 ) + Math.cos (radianLat1) * Math.cos (radianLat2) * Math.pow (Math.sin (radiandiff2 / 2 ),2))); s = s * 6378137.0 ; s = Math.round ( s * 100000 ) / 100000 ; return s ; }

the result is in meters and to convert to kilometers just divide by 1000. that all for now.a programmer's dairy..