JavaScript Calculate Distance Between Latitude Longitude Points
home
Run
screen_rotation
fullscreen
cloud_download
navigate_next
<html> <head> <title>Javascript Demo</title> <script type="text/javascript"> function getDistanceFromLatLonInKm(latitude1,longitude1,latitude2,longitude2,units) { var earthRadius = 6371; // Radius of the earth in km var dLat = deg2rad(latitude2-latitude1); var dLon = deg2rad(longitude2-longitude1); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(latitude1)) * Math.cos(deg2rad(latitude2)) * Math.sin(dLon/2) * Math.sin(dLon/2) ; var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = earthRadius * c; var miles = d / 1.609344; if ( units == 'km' ) { document.getElementById("output").innerHTML = d+"Km"; } else { document.getElementById("output").innerHTML = miles+"Miles"; } } </script> </head> <body> <p>Latitude 1 = 40.7128, Longitude 1 = 74.0059 (New York) </p> <p>Latitude 2 = 51.5074, Longitude 2 = 0.1278 (London)</p> <p>Distance Between London And New York is <span id="output"></span></p> <a href="javascript:void(0)" onclick="getDistanceFromLatLonInKm(40.7128,74.0059,51.5074,0.1278,'km')">Get Distance</a> </body> </html>
<html> <head> <title>Javascript Demo</title> <script type="text/javascript"> function getDistanceFromLatLonInKm(latitude1,longitude1,latitude2,longitude2,units) { var earthRadius = 6371; // Radius of the earth in km var dLat = deg2rad(latitude2-latitude1); var dLon = deg2rad(longitude2-longitude1); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(latitude1)) * Math.cos(deg2rad(latitude2)) * Math.sin(dLon/2) * Math.sin(dLon/2) ; var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = earthRadius * c; var miles = d / 1.609344; if ( units == 'km' ) { document.getElementById("output").innerHTML = d+"Km"; } else { document.getElementById("output").innerHTML = miles+"Miles"; } } </script> </head> <body> <p>Latitude 1 = 40.7128, Longitude 1 = 74.0059 (New York) </p> <p>Latitude 2 = 51.5074, Longitude 2 = 0.1278 (London)</p> <p>Distance Between London And New York is <span id="output"></span></p> <a href="javascript:void(0)" onclick="getDistanceFromLatLonInKm(40.7128,74.0059,51.5074,0.1278,'km')">Get Distance</a> </body> </html>
Copyrights@tutorialsplane.com