-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwritegeolocations.html
More file actions
100 lines (85 loc) · 3.43 KB
/
writegeolocations.html
File metadata and controls
100 lines (85 loc) · 3.43 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html>
<head>
<title>Distances and Bearings</title>
</head>
<body>
<div style="margin: 5%; font-size: 125%">
<h2 style="color: blue">The shortest distances between locations</h2>
<script>
showCity("Helsinki", 60.167, 24.95, "New York", 40.717, -74.0167);
showCity("Helsinki", 60.167, 24.95, "London ", 51.500, 0.133);
showCity("Helsinki", 60.167, 24.95, "Berlin", 52.517, 13.383);
showCity("Helsinki", 60.167, 24.95, "Wien", 48.217, 16.367);
showCity("Helsinki", 60.167, 24.95, "Tallin", 59.433, 24.750);
showCity("Helsinki", 60.167, 24.95, "Stockholm", 59.333, 18.067);
showCity("Helsinki", 60.167, 24.95, "Tokyo", 35.683 , 139.70);
function showCity(city1, latit1, longit1, city2, latit2, longit2)
{
document.write("<p>City " + city1
+ " ( Latitude " + latit1 + '°'
+ " Longitude " + longit1 + '° )');
document.write('<p> City ' + city2
+ " ( Latitude " + latit2 + '°'
+ " Longitude " + longit2 + '° )');
dist = haversine(latit1, longit1, latit2, longit2);
document.write('<p> Distance from ' + city1 + " to "
+ city2 + ' ' + dist);
bearing = newBearing(latit1, longit1, latit2, longit2);
document.write('<p> Initial bearing ' + bearing);
backBear = newBearing(latit2, longit2, latit1, longit1);
document.write('<p> Back bearing ' + backBear);
document.write("<hr>")
}
function haversine(lat1, lon1, lat2, lon2)
{
// distance between latitudes
// and longitudes
let dLat = (lat2 - lat1) * Math.PI / 180.0;
let dLon = (lon2 - lon1) * Math.PI / 180.0;
// convert to radians
φ1 = lat1 * Math.PI / 180.0;
φ2 = lat2 * Math.PI / 180.0;
// apply formulae
let a = Math.pow(Math.sin(dLat / 2), 2) +
Math.pow(Math.sin(dLon / 2), 2) *
Math.cos(φ1) *
Math.cos(φ2);
let radius = 6371; // Earth radius km
let c = 2 * Math.asin(Math.sqrt(a));
let dist = radius* c;
distFix1 = parseFloat(dist.toFixed(1));
return distFix1 + " km"
}
function newBearing(lat1, lon1, lat2, lon2) {
let radians = (v) => v*Math.PI/180;
let φ1 = radians(lat1);
let φ2 = radians(lat2);
let λ1 = radians(lon1);
let λ2 = radians(lon2);
let y = Math.sin(λ2-λ1) * Math.cos(φ2);
let x = Math.cos(φ1)*Math.sin(φ2) -
Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ2-λ1);
let θ = Math.atan2(y, x);
var g = (θ*180/Math.PI + 360) % 360; // in degrees
var wordHeading = ""
if (g > 30 && g < 60) wordHeading = "North-East ↗️ "
if (g > 60 && g < 120) wordHeading = "East ➡️ "
if (g > 120 && g < 150) wordHeading = "South-East ↘️ "
if (g > 150 && g < 210) wordHeading = "South ⬇️ "
if (g > 210 && g < 240) wordHeading = "South-West ↙️ "
if (g > 240 && g < 300) wordHeading = "⬅️ West"
if (g > 300 && g < 330) wordHeading = "North-West ↖️ "
if (g > 330 && g < 360) wordHeading = "North ⬆️ "
if (g >= 0 && g < 30) wordHeading = "North ⬆️ "
return (parseFloat(g.toFixed(2)) + '° ' + wordHeading);
}
</script>
<ul>
<li><a href="https://www.movable-type.co.uk/scripts/latlong.html">About bearings and distances</a></li>
<li><a href="https://www.timeanddate.com/worldclock/distanceresult.html?p1=101&p2=179">Compare this!</a></li>
</ul>
</div>
© J. Lammi 2023
</body>
</html>