commenthol / leaflet-rastercoords

Leaflet plugin for plain image map projection to display large images using tiles generated with gdal2tiles-leaflet
https://commenthol.github.io/leaflet-rastercoords
MIT License
120 stars 23 forks source link

React-leaflet bug with leaflet-rastercoords setMaxboundaries #11

Closed atipezda closed 1 year ago

atipezda commented 2 years ago

What happend?

When using leaflet-rastercoords plugin with react and react-leaflet we have an issue that overrides maxBounds of map.

Steps to reproduce

Create RasterCoords as state

const [raster] = useState<Raster>(new L.RasterCoords(map, imageDimensions))

Set map max Bounds with value that inst same as value for raster layer bounds f.e use padding

map.setMaxBounds(raster.getMaxBounds().pad(1))

Change state or re-render the component and whole bounds are set to max bounds of raster layer (raster-coords overrides our settings on re-render)

Raster-coords doesn't need to set max-bounds

We can just use bounds property of tile layer to limit layer dimensions

<TileLayer
   bounds={raster.getMaxBounds()}
   ...
 />

What is causing this?

That lines of code in leaflet-rastercoords: https://github.com/commenthol/leaflet-rastercoords/blob/564731dc593e24a2f1f173fc736e46fe8f16a5b1/rastercoords.js#L38-L40

Possible fix

Just add new param that can disable setting maxBounds with default value (backward-compability)

L.RasterCoords = function (map, imgsize, tilesize, setmaxbounds = true) {

and than on line with if:

    if (setmaxbounds && this.width && this.height) {
      this.setMaxBounds()
    }