Search code examples
openlayers-3angular-openlayers

Angular OpenLayers - Zoom to box with bounds


I tried to figure out how to zoom the map to box with bounds but couldn't find a good example. I tried with

angular.extend($scope, {
        offset: 0,
        center: {
            lat: 30.0047,
            lon: 31.2586,
            bounds: [30.566461328125,
                     29.766565657014183,
                     31.950738671875005,
                     30.242264176913594]
        }
    });

How can I set themap to zoom to the bounding box without complicate conversion of the 4 bounds to (center, zoom)?


Solution

  • I'm pretty sure you want

    map.getView().fit( 
        ol.proj.transformExtent([ x1, y1, x2, y2 ], 'EPSG:COORDS', 'EPSG:MAP'),
        map.getSize()
        );
    

    This is after your map exists, not on initialization.

    http://openlayers.org/en/master/apidoc/ol.View.html#fit