Leaflet.js fitBounds with padding?

匿名 (未验证) 提交于 2019-12-03 08:35:02

问题:

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)]]; 

回答1:

map.fitBounds(bounds, {padding: []}) should work. I suggest you to change padding to eg. [50, 50], maybe your padding values are too small.

Theck out this JSFiddle exmaple.



回答2:

I came across this problem. As I understand the padding is controlled by the zoom. In my example all padding settings under 10 make no difference. As soon as the padding goes to 10 the map is zoomed out one level and there is padding. Furhter increasing the padding value has no influence, until it is so big that another zoom-out level is reached.



回答3:

FWI, you can also set a max zoom level to the map like so:

var map = L.mapbox.map('map', 'mapbox.emerald', {     maxZoom: 16 }); 

this way you can avoid the map zooming in too much in case the markers are too close togther.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!