Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore marker initial position and zoom level in Leaflet

In Leaflet, I am trying to create a function that restores the initial position and zoom level of a layer containing coordinates. The code below

function refresh() {
    map.fitBounds(coordLayer.getBounds();
}

centers the marker but is zoomed in to the maxZoom I had set:

var map = L.map('map').setView([lat, lon], 6);

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' {
    maxZoom: 13,
    minZoom: 2,
    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);

The default zoom level on page refresh is 6, but I don't know how to create a function that centers the marker at zoom level 6.

like image 307
JAT86 Avatar asked Oct 13 '25 09:10

JAT86


1 Answers

Found a simple solution after reading the Documentation:

function refresh() {
map.setView([lat, lon], 6);
}
like image 183
JAT86 Avatar answered Oct 14 '25 22:10

JAT86