3

How to zoom in map in OpenLayerss 3 on click of button?

I want to zoom in and zoom out the map on custom buttons instead of + and - buttons which are on default map.

I tried with:

onclick="map.zoomIn();"

But it's not working, though it was working in OpenLayers 2.

3 Answers 3

7

If you want to zoom in if you click on the map the following should work:

map.on('click', function() {
    map.getView().setZoom(map.getView().getZoom()+1);    
});
3
  • I want to click on button not on map. This button is custom button.
    – prem30488
    Commented Sep 15, 2015 at 10:23
  • 1
    Thank you. it is working. map.getView().setZoom(map.getView().getZoom()+1);
    – prem30488
    Commented Sep 15, 2015 at 10:24
  • This works, but is very rough, not smooth.
    – lenooh
    Commented Jul 10, 2020 at 15:07
2

If you want a smooth zoom:

map.getView().animate({
  zoom: map.getView().getZoom() + 1,
  duration: 250
})
1
  • I have legacy openlayers code v.3.2.0 and there is no .animate, this code worked: javascript var zoomAnimation = ol.animation.zoom({ resolution: map.getView().getResolution() }); map.beforeRender(zoomAnimation); map.getView().setResolution(map.getView().getResolution() * 2 ); Commented Feb 23, 2023 at 11:19
0

I have legacy openlayers code v.3.2.0 and there is no .animate, this code worked:


var zoomAnimation = ol.animation.zoom({
   resolution: map.getView().getResolution()
});

map.beforeRender(zoomAnimation);
map.getView().setResolution(map.getView().getResolution() * 2 );

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.