0

I am creating the interaction for zoom in and zoom out using OpenLayers. I am able to achieve zoom in but I am not able to figure out how can I create zoom out to some extent. Here is my code for zoom in

$('#zoomin').click(function () {
    removeInteraction();
    $('#map').css("cursor", "zoom-in");
    map.addInteraction(dragBoxInteraction);
    dragBoxInteraction.on('boxend', function () {
        var extent = dragBoxInteraction.getGeometry().getExtent();
        map.getView().fit(extent, map.getSize());
    })
})

For zoom out I have written something like this

$('#zoomout').click(function () {
    removeInteraction();
    map.getView().animate({
        zoom: map.getView().getZoom() - 1,
        duration: 250
    })
})

Using this way I am able to draw a rectangle box and I am getting the extent and I am able to zoom in that extent.

But Using the same feature how will I zoom out?

4
  • 1
    Zoom out to what? You cannot draw rectangle bigger than map view.
    – TomazicM
    Jan 29, 2021 at 8:21
  • True... But I want to draw the rectangle to the current extent and zoom out from that extent.
    – Piyush
    Jan 29, 2021 at 8:30
  • 2
    I admit I still don't understand zoom out to what? Maybe a picture or two would help.
    – TomazicM
    Jan 29, 2021 at 9:20
  • 1
    If you think about initial extent, just pick up central coordinate after map initialization (map.getView().getCenter()) and zoom level and store it in some accessible variable. After that, on zoom out, just use 'zoom in' to that previously stored extent.
    – Kumbra
    Jan 30, 2021 at 14:28

1 Answer 1

3

You can try with this function:

 const handleSquareZoomOut = () => {
    const dragZoom = new ol.interaction.DragZoom({
      condition: ol.events.condition.always,
      out:true,
    })
    
    map.addInteraction(dragZoom)
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.