Changed around line 1
+ // Initialize the map
+ function initMap() {
+ // Create a basic map using Leaflet (open-source)
+ const map = L.map('map').setView([0, 0], 2);
+
+ // Add OpenStreetMap tiles (open-source)
+ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
+ }).addTo(map);
+
+ // Get references to coordinate display elements
+ const latitudeDisplay = document.getElementById('latitude');
+ const longitudeDisplay = document.getElementById('longitude');
+
+ // Add click event to the map
+ map.on('click', function(e) {
+ const { lat, lng } = e.latlng;
+
+ // Update the display with the coordinates
+ latitudeDisplay.textContent = lat.toFixed(6);
+ longitudeDisplay.textContent = lng.toFixed(6);
+
+ // Add a marker at the clicked location
+ if (window.currentMarker) {
+ map.removeLayer(window.currentMarker);
+ }
+
+ window.currentMarker = L.marker([lat, lng]).addTo(map)
+ .bindPopup(`Lat: ${lat.toFixed(6)}
Lng: ${lng.toFixed(6)}`)
+ .openPopup();
+ });
+
+ // Try to get user's current location
+ if (navigator.geolocation) {
+ navigator.geolocation.getCurrentPosition(
+ (position) => {
+ const { latitude, longitude } = position.coords;
+ map.setView([latitude, longitude], 13);
+
+ // Update the display with current location
+ latitudeDisplay.textContent = latitude.toFixed(6);
+ longitudeDisplay.textContent = longitude.toFixed(6);
+
+ // Add a marker at current location
+ window.currentMarker = L.marker([latitude, longitude]).addTo(map)
+ .bindPopup(`Your location
Lat: ${latitude.toFixed(6)}
Lng: ${longitude.toFixed(6)}`)
+ .openPopup();
+ },
+ (error) => {
+ console.log('Geolocation error:', error);
+ // Default to showing coordinates at center of map
+ const center = map.getCenter();
+ latitudeDisplay.textContent = center.lat.toFixed(6);
+ longitudeDisplay.textContent = center.lng.toFixed(6);
+ }
+ );
+ }
+ }
+
+ // Load Leaflet dynamically
+ function loadLeaflet() {
+ return new Promise((resolve) => {
+ const link = document.createElement('link');
+ link.rel = 'stylesheet';
+ link.href = 'https://unpkg.com/leaflet@1.7.1/dist/leaflet.css';
+ link.integrity = 'sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==';
+ link.crossOrigin = '';
+ document.head.appendChild(link);
+
+ const script = document.createElement('script');
+ script.src = 'https://unpkg.com/leaflet@1.7.1/dist/leaflet.js';
+ script.integrity = 'sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==';
+ script.crossOrigin = '';
+ script.onload = resolve;
+ document.head.appendChild(script);
+ });
+ }
+
+ // Initialize the app when DOM is loaded
+ document.addEventListener('DOMContentLoaded', async function() {
+ await loadLeaflet();
+ initMap();
+ });