Search code examples
openlayersopenlayers-7

OpenLayers 7 - Check if coordinate is within layer extent


I'm struggling to get OpenLayers to check whether a coord is within a layer's extent. It took me a while to get it to even check something, but it only ever returns false, even when passing it the center of the extent so I'm clearly doing something wrong.

https://jsfiddle.net/mr_magpie/w405punq/2/

let poly = [-6.2212531, 53.3257768, -6.3122428, 53.3643417];

console.log(ol.extent.getCenter(poly));
console.log(ol.extent.containsCoordinate(poly, ol.extent.getCenter(poly)));
console.log(ol.extent.containsXY(poly, ol.extent.getCenter(poly)));
console.log(ol.extent.containsExtent(poly, ol.extent.getCenter(poly)));

These (as far as I can tell) are the methods to check points/extents but I can't seem to get any working. This prev question is what I tried to get working https://stackoverflow.com/a/34949094

I'm displaying different TileLayers over a map as overlays and if a point is outside a certain layer's extent then I don't want to display that layer so I want to check whether a point is within or outside a given extent.

Any ideas? Many thanks


Solution

  • An extent should be in the form [minX, minY, maxX, maxY]

    [-6.2212531, 53.3257768, -6.3122428, 53.3643417] is not valid

    [-6.3122428, 53.3257768, -6.2212531, 53.3643417] is valid