mapbox / mapbox/mapbox-gl-js

mousemove with two layers on top of each other (event propagation)

Open
#12,016 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged docs :scroll:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

2.8.2

### Question

The closest to my question are: https://stackoverflow.com/questions/50294242/mapbox-gl-js-overlapping-layers-and-mouse-event-handling and https://stackoverflow.com/questions/48555583/how-to-ignore-mouse-events-on-a-mapbox-layer/ - but they don't answer it.

I have two layers, let's imagine one is for a country, and another is for a city. Both of them a handled like this:

// CountryLayer.js
map.on("mousemove", "country-layer", e => {
// show info about the country
featureProps = e.features[0] // display some notification from the props
...

// CityLayer.js
map.on("mousemove", "city-layer", e => {
// show info about the city
featureProps = e.features[0] // display some notification from the props
...

It's done in different components. But when I mouseover `city-layer` mapbox thinks that I'm still "mousemoving" on top of the `country-layer` as well, so I get two notifications from separate components, where I need only one - in that case from the `city-layer` cause it's on top of `country-layer`.

Handling the `mousemove` without layerId in one place is gonna be a mess and breaks all the good rules about programming. Creating external "event manager" which will track whether I'm hovering the city and if is so will remove `mousemove` event from `country-layer` - is complex. I didn't find any good alternatives and there 0 total information about it in documentation. At least, I would be glad to disable pointer events for a layer like this:

map.on("mousemove", "city-layer", e => {
map.getLayer("country-layer").setStyle({ "pointer-events": false })
featureProps = e.features[0]
...

or something like this. Is it possible? Is there more adequate way around it?

### Links to related documentation

https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mousemove
https://docs.mapbox.com/mapbox-gl-js/api/map/#map#on

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Map event:mousemove and Map#on documentation, then reproduce the two-layer example from the issue. Determine whether overlapping layer handlers can be ordered or stopped independently, and document or implement the supported behavior. Done means the city-layer interaction no longer produces the unwanted country-layer notification, with the behavior explained in the relevant documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.