限制地图缩放

This commit is contained in:
2026-06-04 14:28:02 +08:00
parent 5b6a1a60a1
commit c3cdcfd379
+13 -6
View File
@@ -30,6 +30,12 @@ let map: L.Map | null = null
let markerLayer: L.LayerGroup | null = null let markerLayer: L.LayerGroup | null = null
let hasFitBounds = false let hasFitBounds = false
const minMapZoom = 3
const worldBounds = L.latLngBounds(
[-85.05112878, -180],
[85.05112878, 180],
)
onMounted(async () => { onMounted(async () => {
window.addEventListener('click', closeNodeMenu) window.addEventListener('click', closeNodeMenu)
window.addEventListener('keydown', handleKeydown) window.addEventListener('keydown', handleKeydown)
@@ -39,15 +45,16 @@ onMounted(async () => {
} }
map = L.map(mapEl.value, { map = L.map(mapEl.value, {
zoomControl: true, zoomControl: true,
maxBounds: [ minZoom: minMapZoom,
[-85, -180], maxBounds: worldBounds,
[85, 180],
],
maxBoundsViscosity: 1.0, maxBoundsViscosity: 1.0,
worldCopyJump: false, worldCopyJump: false,
}).setView([0, 0], 2) }).setView([0, 0], minMapZoom)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
minZoom: minMapZoom,
maxZoom: 19, maxZoom: 19,
noWrap: true,
bounds: worldBounds,
attribution: '© OpenStreetMap contributors', attribution: '© OpenStreetMap contributors',
}).addTo(map) }).addTo(map)
map.on('click', () => { map.on('click', () => {
@@ -179,7 +186,7 @@ function buildClusterMarker(cluster: MapClusterNode): L.Marker {
marker.on('click', () => { marker.on('click', () => {
closeNodeMenu() closeNodeMenu()
if (map) { if (map) {
map.setView([cluster.latitude, cluster.longitude], Math.min(map.getZoom() + 2, map.getMaxZoom())) map.setView([cluster.latitude, cluster.longitude], Math.max(minMapZoom, Math.min(map.getZoom() + 2, map.getMaxZoom())))
} }
}) })
return marker return marker