1

I have angular application that uses https://www.npmjs.com/package/angular-split. 1 split area has open layers map. If only map split area is visible, following method:

private zoomToExtent(extent: IExtent, view: ol.View) {
  const mapSize = this.map.getSize();
  this.map.getView().fit(extent, { size: mapSize });
}

works just fine. However when the window has multiple splits open it does not work because mapSize is still the same even though the map is smaller.

The map works otherwise ok because map buttons and scale line on the right bottom adapts to opening other split areas.

So how this should be done i.e. how to get real map size?

char m
  • 7,840
  • 14
  • 68
  • 117

1 Answers1

1

Maybe calculateExtent suits well:

Calculate the extent for the current view state and the passed size. The size is the pixel dimensions of the box into which the calculated extent should fit. In most cases you want to get the extent of the entire map, that is map.getSize().

Therefore something like:

const currentExtent = this.map.getView().calculateExtent(this.map.getSize());

Edit: Good point from @Mike:

updateSize might be even better:

Force a recalculation of the map viewport size. This should be called when third-party code changes the size of the map viewport.

private zoomToExtent(extent: IExtent, view: ol.View) { // Parameter view unnecessary (?)
  this.map.updateSize();
  this.map.getView().fit(extent, { size: this.map.getSize() });
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • 1
    Thanks! Parameter is there because in actual method it's used. In real code i will call updateSize whenever split areas visibility/size changes. – char m Feb 26 '20 at 13:00