-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
44 lines (37 loc) Β· 1.4 KB
/
map.js
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
//map
const map = L.map('map').setView([39.8283, -98.5795], 4);
// OpenStreetMap layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let marker;
document.getElementById("search").addEventListener("click", async () => {
const address = document.getElementById("address").value;
if (address) {
await geocodeAddress(address);
} else {
alert("Please enter an address or city.");
}
});
async function geocodeAddress(address) {
const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(address)}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.length > 0) {
const { lat, lon } = data[0];
// Center map on the searched location
map.setView([lat, lon], 14);
// Place a marker
if (marker) {
map.removeLayer(marker);
}
marker = L.marker([lat, lon]).addTo(map);
} else {
alert("No results found for the provided address.");
}
} catch (error) {
console.error("Error fetching geocode data:", error);
alert("There was an error with the geocoding request.");
}
}