Jump to content

MediaWiki:PlaceForm.js

From Costa Sano MediaWiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
function lookupCoordinates() {
    const address = document.querySelector('[name="address"]').value;
    if (!address) {
        alert("Please enter an address first.");
        return;
    }

    const url = "https://nominatim.openstreetmap.org/search?format=json&q=" + encodeURIComponent(address);

    fetch(url)
        .then(response => response.json())
        .then(data => {
            if (data.length === 0) {
                alert("No coordinates found for this address.");
                return;
            }
            const lat = data[0].lat;
            const lon = data[0].lon;

            document.querySelector('[name="latitude"]').value = lat;
            document.querySelector('[name="longitude"]').value = lon;
        })
        .catch(error => alert("Error fetching coordinates: " + error));
}