-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDistance.html
39 lines (39 loc) · 1.68 KB
/
getDistance.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<html>
<head>
<title>Get Distance Practice</title>
<script>
function getDistance(){
alert('getting distance');
navigator.geolocation.getCurrentPosition(getDistanceFromPoint);
}
function getDistanceFromPoint(position){
var lat = 51.524616;
var lng = -0.13818;
var distance = calculateDistance(position.coords.latitude, position.coords.longitude, lat, lng, 'K'); // get distance in Km
document.getElementById('showDistance').innerHTML = "Distance: " + distance.toFixed(2);
}
// distance between two points
function calculateDistance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var radlon1 = Math.PI * lon1/180;
var radlon2 = Math.PI * lon2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var subAngle = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
subAngle = Math.acos(subAngle);
subAngle = subAngle * 180/Math.PI;
dist = (subAngle/360) * 2 * Math.PI * 3956;
if (unit=="K") { dist = dist * 1.609344 ;}
// where radius of the earth is 3956 miles
if (unit=="N") { dist = dist * 0.8684 ;}
return dist;
};
</script>
</head>
<body>
<b>This code will display your current location</b>
<div id='showDistance'>Distance to Warren street in km will be displayed here</div>
<button id ='geoDistance' onclick=getDistance()>Click Me to Calculated Distance to Warren street in km</button>
</body>
</html>