I'm trying to set the fitBounds option in a leaflet map like this:
var bounds = new L.LatLngBounds([[Math.max(lat, borneLat), Math.max(lng, borneLng)], [Math.min(lat, borneLat), Math.min(lng, borneLng)]]); map.fitBounds(bounds, { padding: [20, 20] });
However this doesn't seem to add any padding ? at least the map "zoom level" doesn't change, and it's always showing the two points in the edges (i.e. one is top far right and the other is bottom far left). For some marker sets, it works ok, the zoom is at a decent level away and you can see everything nicely. However when the items are quite close to each other, it seems it zooms in a bit too much unless I can fit it with some "padding."
Also I'm unsure if the bounds would be correct like that? Should I use instead just:
var bounds = [[Math.max(lat, borneLat), Math.max(lng, borneLng)], [Math.min(lat, borneLat), Math.min(lng, borneLng)]];
map.fitBounds(bounds, {padding: []})
should work. I suggest you to change padding to eg. [50, 50]
, maybe your padding values are too small.
Check out this JSFiddle example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With