Friday, 8 September 2017

javascript - OpenLayers - redrawing map after container resize


In my web application, I want to enable users to set the size of the map container.


Everything worked fine when the container was expanded slightly (apparently this is because the tiles that were just behind the border were already loaded). However, when the container is expanded significantly (in the following example, from 300 to 1000px in width), there is blank space left.


How to make the map redraw and adapt to the new size?


Calling redraw() on all layers didn't help. Neither did zooming in and out.


I tested this with the described results in Opera, Chrome and Firefox. In IE8, surprisingly, the problem didn't occur and the map automatically adapted.


A simplified webpage for testing:


















Answer



You need to call the API to update map size.


http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.updateSize



Here's how we do it


window.onresize = function()
{
setTimeout( function() { map.updateSize();}, 200);
}

You can see we did a slight delay, and honestly I don't remember the exact reason why, but we had to give it a slight wait before calling API to resize itself.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...