openlayersopenlayers-5angular-openlayersopenlayers-6

OpenLayers increase or decrease extent by a percentage


I have a map that has features on it and when I zoom to those features the user has the ability to set a preference as to the zoom buffer.

I'm not sure if there is a built-in way of doing this in OL...I've looked at the documentation but couldn't see anything...not sure if I just missed it?

zoomToSelectedFeatures() {
view.extent = MapOverlay.getSource().getExtent();
map.getView().fit(view.extent, MapValues.map.getSize());
}

Wondering if there is a way to plug a number in for say 20% that would make the zoom 20% larger?


Solution

  • The fit method has a padding option but that is not expressed as a percentage so you would need to calculate padding based on the map size. The easier way might be to scale the extent by converting to a geometry as fit can take either an extent or a geometry

    geom = ol.geom.Polygon.fromExtent(view.extent)
    geom.scale(1.2);
    map.getView().fit(geom, {size: MapValues.map.getSize()});