-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlipIt.html
52 lines (43 loc) · 1.69 KB
/
FlipIt.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
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<head>
<title>Antipodes Finder</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
</head>
<body>
<div id="map" style="width: 100%; height: 400px;"></div>
<button onclick="flipIt()">Flip It!</button>
<p id="result"></p>
<script>
var map;
var marker;
var latLng;
function initializeMap(latLng) {
map = L.map('map').setView([latLng.lat, latLng.lng], 10);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19
}).addTo(map);
marker = L.marker([latLng.lat, latLng.lng], {
draggable: true
}).addTo(map);
}
function flipIt() {
var oppositeLat = -marker.getLatLng().lat;
var oppositeLng = marker.getLatLng().lng > 0 ? marker.getLatLng().lng - 180 : marker.getLatLng().lng + 180;
var oppositeLatLng = { lat: oppositeLat, lng: oppositeLng };
map.setView([oppositeLat, oppositeLng]);
marker.setLatLng(oppositeLatLng);
document.getElementById('result').textContent = 'The opposite of ' + marker.getLatLng().lat.toFixed(2) + ',' + marker.getLatLng().lng.toFixed(2) + ' on earth is ' + oppositeLat.toFixed(2) + ',' + oppositeLng.toFixed(2);
}
$(document).ready(function() {
$.getJSON('https://ipinfo.io/json', function(data) {
var coords = data.loc.split(",");
latLng = { lat: parseFloat(coords[0]), lng: parseFloat(coords[1]) };
initializeMap(latLng);
});
});
</script>
</body>
</html>