Patrick Cailly
Patrick Cailly

Reputation: 21

How can you set the extent of a XYZ source using OpenLayers 6?

I have a map with a background layer of Europe and another ( XYZ ) layer displaying a much smaller area.

How can I avoid the 404 error message from the XYZ layer for tiles that don't exist?

I have tried to pass:

extent: [-0.795668404302292,-0.7037491016945445,48.78018752203186,48.83999044180076 ]

But it doesn't work.

Upvotes: 2

Views: 2115

Answers (1)

Mike
Mike

Reputation: 17982

In most cases it is easier to set an extent on the layer.

To limit the extent of a source you must give it a custom tile grid, for example

var defaultTileGrid = createXYZ();

var source = new XYZ({
  url: .... ,
  tileGrid: new TileGrid({
    origin: defaultTileGrid.getOrigin(0),
    resolutions: defaultTileGrid.getResolutions(),
    extent: extent
  })
});

The bounds must be in the same projection as the source

So for EPSG:4326 you would need

var defaultTileGrid = createXYZ({extent: getProjection('EPSG:4326').getExtent()});

var source = new XYZ({
  url: .... ,
  tileGrid: new TileGrid({
    origin: defaultTileGrid.getOrigin(0),
    resolutions: defaultTileGrid.getResolutions(),
    extent: [-0.795668404302292, 48.78018752203186, -0.7037491016945445, 48.83999044180076] 
  })
});

Upvotes: 1

Related Questions