Search code examples
mapboxtilemapbox-gl-js

Get coordinate of selected tileset tile (by expression)


I am able to find and change the properties of a vector tileset using expression, for example:

map.setPaintProperty('my-shp', 'fill-opacity', [
    'match',
    ['get', 'NO'],
    13708, 1,
    /* else */ 0
]);

I was wondering if there is a way to get the exact coordinate of the tileset item using expression?

Sorry if I am wrong with the terminology (of tileset/tile item), feel free to correct me. Thanks


To clarify, using the above expression, I can change the opacity of the tileset item where NO is 13708. My question is, is there a way to get the lat/long coordinate of the tileset item where NO is 13708?


Solution

  • You could just iterate over all rendered features and test the "NO" property then return the coordinates of this feature. To get all rendered features you need to use queryRenderedFeatures. Features follow the GeoJSON structure.

    For example:

    let features = map.queryRenderedFeatures({ layers: ['my-shp'] });
    let filtered = features.filter(feature => {
        return feature.properties.NO === 13708;
    });
    
    console.log(filtered);