-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (28 loc) · 1.15 KB
/
Copy pathscript.js
File metadata and controls
33 lines (28 loc) · 1.15 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
// Initialize map
var map = L.map('map').setView([20.5937, 78.9629], 5); // Default India
// Add OpenStreetMap tiles
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap contributors' }).addTo(map);
var marker;
async function findLocation() {
const query = document.getElementById("location-input").value;
if (!query) {
alert("Please enter a location!");
return;
}
const url = `https://nominatim.openstreetmap.org/search?format=json&q=${query}`;
const response = await fetch(url);
const data = await response.json();
if (data.length > 0) {
const lat = data[0].lat;
const lon = data[0].lon;
const displayName = data[0].display_name;
if (marker) {
map.removeLayer(marker);
}
marker = L.marker([lat, lon]).addTo(map).bindPopup(`<b>${displayName}</b>`).openPopup();
map.setView([lat, lon], 13);
document.getElementById("details").innerHTML = `📍 <b>Location:</b> ${displayName} <br>🌐 <b>Latitude:</b> ${lat} <br>🌐 <b>Longitude:</b> ${lon}`;
} else {
alert("Location not found!");
}
}