0

I am moving code from OpenLayers 3 to OpenLayers 4 (typescript)

I had this piece of code that would extend my extent so that all vector shapes I drew would be visible when the map loaded...

angular.forEach(webMapValues.vectorFieldLayer, function (Field, key) 
{
webMapValues.mapObj.addLayer(Field);
webMapValues.extent = Field.getSource().getExtent();
webMapValues.totalExtent = ol.extent.extend(webMapValues.extent, 
webMapValues.totalExtent);
})

in the above "ol.extent.extend" performed the magic for me and in OpenLayers 4 (typescript) I haven't been able to find the equivalent?

Any help is greatly appreciated!

2 Answers2

1

i believe the function is still there, only the syntax has changed:

import {extend} from 'ol/extent';

const largeExtent = extend(extent1, extent2);
Rob
  • 651
  • 3
  • 14
  • I'm confused...I didn't see "extend" in "ol/extent" so I npm installed "ol" again to see if maybe there was an updated version... got "ol@5.3.1" but I do not have "extend" as a function inside "ol/extent". I have "corner" and "relationship"? Any Ideas on what version I should be using? – Funn_Bobby Mar 14 '19 at 13:46
  • i am confused too... i just tried ol.extent.extend(extend1, extend2) with ol 5.3.0 from the quickstart guide, it seems to work like intended https://openlayers.org/en/latest/doc/quickstart.html – Rob Mar 14 '19 at 13:58
  • Okay, I think it's in how this gets imported. In node_modules I have folder ol and under that I have a folder named "extent" and also a .js file named "extent.js" if I import "ol/extent" it points at the folder...not sure how to get it to point at the .js file...maybe just add the ".js"? – Funn_Bobby Mar 14 '19 at 14:48
  • do you still get the error, also with imports? extend is not a method of extent anymore, it's more or less a standalone function – Rob Mar 15 '19 at 11:43
  • I can't figure out how to use the extent.js file – Funn_Bobby Mar 15 '19 at 19:34
0

I'm using React, typescript and OL4 and found this works for me

import extent from 'ol/extent';

const combinedExtent = extent.extend(<layer1>,<layer2>);
BStone
  • 220
  • 2
  • 9