Set max zoom according to map center

This commit is contained in:
EpicKiwi 2026-06-10 01:10:04 +02:00
parent a65b1981af
commit 72c7a51594
Signed by: epickiwi
GPG key ID: C4B28FD2729941CE

View file

@ -49,10 +49,10 @@ export async function init(){
// Polygone définissant les limites de la carte // Polygone définissant les limites de la carte
fetch("./couches/emprise.geojson").then(res => res.json()).then(geojson => { fetch("./couches/emprise.geojson").then(res => res.json()).then(geojson => {
let coordinate_list = geojson.features[0].geometry.coordinates[0] let coordinate_list = geojson.features[0].geometry.coordinates[0]
let first_point = map.project(L.latLng([...coordinate_list[0]].reverse())) let first_point = L.latLng([...coordinate_list[0]].reverse())
let bounds = L.bounds(first_point, first_point) let bounds = L.latLngBounds(first_point, first_point)
for(let i = 1; i<coordinate_list.length; i++){ for(let i = 1; i<coordinate_list.length; i++){
let point = map.project([...coordinate_list[i]].reverse()) let point = [...coordinate_list[i]].reverse()
bounds.extend(point) bounds.extend(point)
} }
map_bounds = bounds map_bounds = bounds
@ -60,6 +60,14 @@ export async function init(){
}), }),
]) ])
map.addEventListener("move", () => {
if(map_bounds.contains(map.getCenter())){
map.setMaxZoom(21)
} else {
map.setMaxZoom(18)
}
})
// La couche Openstreetmap standard // La couche Openstreetmap standard
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
@ -84,7 +92,7 @@ export async function init(){
).addTo(map) ).addTo(map)
// Parametrage de la carte initiale // Parametrage de la carte initiale
map.setMaxBounds(map_bounds) //map.setMaxBounds(map_bounds)
map.setView(map_center, 19, { map.setView(map_center, 19, {
animate: false, animate: false,
}) })