3

I have an OpenLayers map container defined as

#preview_cont {
    width: 100%;
    height: 15cm;
}

It looks OK on screen, but when I print it, the map is distorted. I assume it's because map container proportions are changed when printing. As far as I know, in such a case

ol.Map.updateSize()

has to be called. So I tried this function:

$(window).resize(function () {
        map.updateSize();
});

Unfortunately, it's not called for printing. Is there a way, how to do it?

1 Answer 1

1

The approach I use is to:

  1. Adapt the size of the view with something like:

    var orgSize = map.getSize();
    var newSize = [newWidth, newHeight];
    map.setSize(newSize);
    map.getView().fit(extent, {size: newSize});
    
  2. Start printing when the map is completly rerendered:

    map.once('rendercomplete', function(event) { 
        startPrint();
    
  3. And finally render it again with the original size:

        map.setSize(orgSize);
        map.getView().fit(extent, {size: size});
    } );
    
2
  • I'm getting extent is not defined error, do you have any ideas ?
    – Capan
    Commented May 20, 2019 at 10:21
  • @Capan, you need to use your own extent. geraldo just gave you an example. Commented Jun 24, 2021 at 5:38

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.