36436-vm/assets/js/city-finder.js
Flatlogic Bot 27c6849991 v4
2025-11-28 18:04:41 +00:00

19 lines
755 B
JavaScript

function getCity() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
// Using a free reverse geocoding API
fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lon}`)
.then(response => response.json())
.then(data => {
if (data.address && data.address.city) {
document.getElementById('city').value = data.address.city;
}
})
.catch(err => console.error("Error fetching city:", err));
});
}
}
window.onload = getCity;