Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map.setStyle() followed by mouse event sometimes causes layer not found error #1183

Closed
caldwellc opened this issue Jul 11, 2023 · 2 comments · Fixed by #1194
Closed

map.setStyle() followed by mouse event sometimes causes layer not found error #1183

caldwellc opened this issue Jul 11, 2023 · 2 comments · Fixed by #1194
Labels

Comments

@caldwellc
Copy link
Contributor

When map.setStyle() is called and a mousemove event fires, mapbox-gl-draw's event listener will run map.queryRenderedFeatures passing the layers from ctx.options.styles. However, this sometimes occurs before mapbox-gl-draw's map.on("data") event listener can add back in the missing style layers caused by the map.setStyle() function, the following error occurs:

mapbox-gl.js:31 Error: The layer 'gl-draw-polygon-fill-inactive.cold' does not exist in the map's style and cannot be queried for features.
at i.queryRenderedFeatures (mapbox-gl.js:35:133328)
at r.queryRenderedFeatures (mapbox-gl.js:35:323577)
at ot (mapbox-gl-draw.js:1:4660)
at Object.click (mapbox-gl-draw.js:1:4319)
at rt (mapbox-gl-draw.js:1:5068)
at i.mousemove (mapbox-gl-draw.js:1:22101)

mapbox-gl-js version 1.13.2
mapbox-gl-draw version 1.3.0, 1.4.2

A suggested solution might be to update the featuresAt function to filter the queryParams.layers to only use layers that exist on the map:

if (ctx.options.styles) queryParams.layers = ctx.options.styles.map(s => s.id);

to

if (ctx.options.styles) queryParams.layers = ctx.options.styles.map(s => s.id).filter(id => ctx.map.style.getLayer(id) != null);

@lemmingapex
Copy link

Is there any update on this? Would you like me to submit a MR for this @stepankuzmin?

@stepankuzmin
Copy link
Contributor

Hi @lemmingapex,

Sure, PRs are welcome!

caldwellc added a commit to caldwellc/mapbox-gl-draw that referenced this issue Aug 16, 2023
…s exist before querying against them. updated existing test cases to account for this check and added a new test case to ensure missing layers are not included in the queryRenderedFeatures call.
stepankuzmin pushed a commit that referenced this issue Aug 21, 2023
* issue #1183 updated featuresAt function to check if style layers exist before querying against them. updated existing test cases to account for this check and added a new test case to ensure missing layers are not included in the queryRenderedFeatures call.

* use map.getLayer instead of map.style.getLayer. update tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants