1

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)?

Jose Gómez
  • 3,110
  • 2
  • 32
  • 54
saledo
  • 23
  • 6

1 Answers1

1

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

ryansstack
  • 1,396
  • 1
  • 15
  • 33
  • 1
    You forgot `map.getSize()`. So right answer would be `map.getView().fit(ol.proj.transformExtent( [ x1, y1, x2, y2 ], 'EPSG:COORDS', 'EPSG:MAP'), map.getSize());`. This is for OpenLayers v3.7 and above, and before was used function fitExtent instead fit. Just update your answer with this. Thank you ;) – saledo Sep 29 '15 at 16:19
  • Done, thanks for catching that. I added a link to the docs too – ryansstack Sep 30 '15 at 16:05