Check if a point or a click landed on a specific layer #6162
-
I have been doing some experiments and i tried to limit clicks/points from landing on "ocean/water" layers and i tried this method. // vuejs
map.value = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [0, 0],
zoom: 1
})
const addPointIfNotWater = (lngLat) => {
const point = map.value.project(lngLat)
const waterFeatures = map.value.queryRenderedFeatures(point, { layers: ['crimea-fill'] })
// debugger
if (waterFeatures.length > 0) {
alert("Cannot place fire on water!")
return
}
features.value.push({
type: 'Feature',
geometry: { type: 'Point', coordinates: [lngLat.lng, lngLat.lat] }
})
map.value.getSource('points').setData({ type: 'FeatureCollection', features: features.value })
} But it seems after reading some documentations that this will never work , unless at each possible point of the layer there was a feature to "fire" this if statement . Is there really no way to simply check if a point belongs to a specific layer ? The simplest use-case is to limit points from being created on top of bodies of water. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I'm not sure I fully follow, but you can |
Beta Was this translation helpful? Give feedback.
I feel the best way to accomplish this might depend on the map's data/style.
For instance, in https://demotiles.maplibre.org/style.json used above, there is no explicit style layer for water — note how the water's colour #D8F2FF comes from the style layer with ID
background
. (It does not come fromcrimea-fill
; you can play around with the style at https://maputnik.github.io/editor/?style=https://demotiles.maplibre.org/style.json to confirm this.)This means that when passing the click event coords to
Map.queryRenderedFeatures
, it will: