stevage / map-gl-utils

A utility library that makes Mapbox GL JS or Maplibre GL a bit more convenient to work with.
https://npmjs.com/package/map-gl-utils
210 stars 24 forks source link

clickOneLayer callback is not called with click event #6

Closed lucasprograms closed 4 years ago

lucasprograms commented 4 years ago

hey @stevage, it's Lucas from vetro fiber map

Loving this lib so far. I have forked the lib to add functionality to the clickOneLayer method. Specifically, it passes the click event to the cb, and adds a param for a seperate callback that triggers if there is no matching layer:

clickOneLayer(layers, cb, noMatchCb) {
            map.on('click', e => {
                let match = false;
                for (const layer of layers) {
                    const features = map.queryRenderedFeatures(e.point, { layers: [ layer ] });
                    if (features[0]) {
                        cb(e, {
                            layer,
                            feature: features[0],
                            features,
                        });
                        match = true;
                        break;
                    }
                }

                if (!match && noMatchCb) {
                    noMatchCb(e);
                }
            });
        },

is this something you'd be interested in having in the lib?

stevage commented 4 years ago

Thanks for the suggestion!

The way you have this:

                        cb(e, {
                            layer,

would break existing use of the function. But if you change it to:

                        cb({
                            event: e,
                            layer,

then it looks good.

stevage commented 4 years ago

Thanks for the PR!