Friday, 10 May 2019

javascript - OpenLayers-3 setting zoom so layers fit in map window


Is there a way to dynamically set the zoom level so that the current layers always fit within the width of the map frame on page load?


Currently the zoom level is set statically such that when using a full resolution monitor the application loads perfectly the entirety of the map fits but when loading the map at a lower resolution the map loads at the same zoom level but features are cut off.



Answer



You can adjust the map's view to fit an extent with: http://openlayers.org/en/v3.6.0/apidoc/ol.View.html#fitExtent


Use getExtent() to retrieve your layers' extent: e.g. http://openlayers.org/en/v3.6.0/apidoc/ol.source.Vector.html#getExtent


See Tim's answer at https://stackoverflow.com/questions/30121024/openlayers-3-zoom-to-combined-extent



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...