Rodrigo5244
Rodrigo5244

Reputation: 5525

How to fit the view to all features in a source of vector tiles?

Using openlayers version 4, how do I fit the view to all features in a source of vector tiles?

After I identified the source is ready by listenting to the tileloadend event. I can't use map.getView().fit(vectorLayer.getSource().getExtent(), map.getSize()); because getExtent is not a function. But I found other examples where people are able to get the extent of sources. Is this a limitation of vector tiles sources? If so, is there a way to work around this limitation, maybe listing all the features of the source?

Here is the code I have so far: jsbin

Upvotes: 0

Views: 1166

Answers (2)

nalm
nalm

Reputation: 410

use the

var extent = vectorTile.getExtent()

and after use it to fit

map.getView().fit(extent, { options });

Upvotes: 0

pavankguduru
pavankguduru

Reputation: 325

getExtent method is for layers not the source. And there is no getFeatures method available for vector tiles.

vectorLayer.getSource().once('tileloadend', function (evt) {
    if (vectorLayer.getSource().getState() === 'ready') {
        map.getView().fit(vectorLayer.getExtent(), map.getSize());
        console.info(map.getView().getCenter());
        console.info(map.getView().getZoom());
    }
})

I've modified your code and checked, the layer still returns undefined extent.

Upvotes: 1

Related Questions