-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
56 lines (47 loc) · 1.81 KB
/
index.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
53
54
55
56
<!DOCTYPE html>
<html>
<head>
<title>OpenStreetMap Tile</title>
</head>
<body>
<p id="locationInfo"></p>
<script>
function getLocation() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(onLocationReceived, onError);
} else {
console.log("Geolocation is not available on this device.");
}
}
function onLocationReceived(position) {
// Get latitude and longitude from the geolocation data
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// Convert latitude and longitude to OpenStreetMap tile format
const zoomLevel = 15;
const [xTile, yTile] = latLonToTile(latitude, longitude, zoomLevel);
// Open a new window to the OpenStreetMap tile
const tileURL = `https://tile.openstreetmap.org/${zoomLevel}/${xTile}/${yTile}.png`;
window.open(tileURL, "_blank");
// Update the location information on the page
const locationInfoElement = document.getElementById("locationInfo");
locationInfoElement.innerText = `Latitude: ${latitude.toFixed(6)}\nLongitude: ${longitude.toFixed(6)}`;
// Close the current window after a short delay (e.g., 5 seconds)
setTimeout(() => {
window.close();
}, 5000);
}
function onError(error) {
console.log("Error occurred while retrieving geolocation:", error);
}
function latLonToTile(lat, lon, zoom) {
const n = 2.0 ** zoom;
const xTile = Math.floor(((lon + 180) / 360) * n);
const yTile = Math.floor(((1 - Math.log(Math.tan(lat * (Math.PI / 180)) + 1 / Math.cos(lat * (Math.PI / 180))) / Math.PI) / 2) * n);
return [xTile, yTile];
}
// Automatically execute the geolocation function when the page loads
getLocation();
</script>
</body>
</html>