Last active
March 16, 2025 17:01
-
-
Save thinkphp/2e2bf2e675fccdc5176ab1b31fca483a to your computer and use it in GitHub Desktop.
show.map.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to show map using OpenStreetMap with Leaflet | |
let map; | |
let marker; | |
function showMap(lat, lng, name) { | |
mapContainer.style.display = 'flex'; | |
// If the map doesn't exist yet, create it | |
if (!map) { | |
map = L.map('map').setView([lat, lng], 15); | |
// Add OpenStreetMap tile layer | |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { | |
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', | |
maxZoom: 19 | |
}).addTo(map); | |
// Add control for fullscreen option | |
L.control.zoom({ | |
position: 'topleft' | |
}).addTo(map); | |
} else { | |
// If map already exists, just update the view | |
map.setView([lat, lng], 15); | |
} | |
// Remove existing marker if any | |
if (marker) map.removeLayer(marker); | |
// Add new marker | |
marker = L.marker([lat, lng]).addTo(map) | |
.bindPopup(`<strong>${name}</strong>`) | |
.openPopup(); | |
// Fix for map not displaying correctly in hidden container | |
setTimeout(function() { | |
map.invalidateSize(); | |
}, 100); | |
} | |
// Close map | |
mapClose.addEventListener('click', function() { | |
mapContainer.style.display = 'none'; | |
}); | |
// Search button click handler | |
searchButton.addEventListener('click', async function() { | |
const location = locationInput.value.trim(); | |
const category = categorySelect.value; | |
const limit = limitInput.value || 9; | |
if (!location) { | |
alert('Please enter a location'); | |
return; | |
} | |
if (!foursquareApiKey) { | |
apiKeyModal.style.display = 'flex'; | |
return; | |
} | |
const places = await fetchPlaces(location, category, limit); | |
displayPlaces(places); | |
}); | |
// Allow pressing Enter in the location input to trigger search | |
locationInput.addEventListener('keypress', function(e) { | |
if (e.key === 'Enter') { | |
searchButton.click(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment