Use openlayers extent with angular

1.2k Views Asked by At

I use angular 6 with openlayers 5.1.3. I try to combine the extent of two vector layers and then fit the view of the map.

I do the following

import Extent from 'ol/interaction/Extent.js';
olextent: Extent;
//then in ngOnInit
ngOnInit() {
  this.olextent = new Extent();
}

//then get the extent of the 2 layers
let relatedext = this.relatedsource.getExtent();
let vectorext = this.vectorsource.getExtent(); 
//then create an empty extent and extent it with the layer extents

ext = this.olextent.createEmpty(); 
ext.extend(this.olextent, relatedext);
ext.extend(this.olextent, vectorext); 

//also create a size and use it with the extent to fit the map view

this.olmap.getView().fit(ext, {size:size, duration: 1500});

This code looks normal to me, but I get this.olextent.createEmpty is not a function and it does not work.

How can I fix this?

1

There are 1 best solutions below

1
On BEST ANSWER

I think this is what you may be trying to achieve (unless you have some interaction processing elsewhere in your code)

import {createEmpty, extend} from 'ol/extent.js';

//then in ngOnInit
ngOnInit() {
  this.olextent = createEmpty();
}

//then get the extent of the 2 layers
let relatedext = this.relatedsource.getExtent();
let vectorext = this.vectorsource.getExtent(); 
//then extent empty extent with the layer extents

extend(this.olextent, relatedext);
extend(this.olextent, vectorext); 

//also create a size and use it with the extent to fit the map view

this.olmap.getView().fit(this.olextent, {size:size, duration: 1500});