简体   繁体   中英

Leaflet.js shrink getBounds()

Leafletjs has a function pad(x) for increasing the latLng bounds. How do I shrink it? I've tried putting a negative number in there with little success.

I'm basically trying to see if a marker is within the current view, and if not center on that marker. But some markers can be plotted right on the edge of view and still be classed as in view. I'm trying to add inner padding on the current viewport bounds to avoid this.

if(!map.getBounds().pad(-1.5).contains(marker.getLatLng()))

You can use the paddingTopLeft , paddingBottomRight or padding options of L.Map 's fitBounds method:

Sets the amount of padding in the top left corner of a map container that shouldn't be accounted for when setting the view to fit bounds. Useful if you have some control overlays on the map like a sidebar and you don't want them to obscure objects you're zooming to.

http://leafletjs.com/reference.html#map-paddingtopleft

map.fitBounds(layer, {'padding': [n, n]})

The negative value range for pad() is 0 to -0.5.

https://github.com/Leaflet/Leaflet/issues/5741

edit: link basically states that pad takes in a ratio of 1 not a typical percentage value. So to decrease the bounds by 50% (which would visually make it 0, because the left bounds would go in 50% to the middle, and the right bounds would also go in 50% to the middle, etc) the parameter value would be pad(-0.5) NOT pad(-50)

to increase add a positive ratio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM