3

I am currently working on an interactive map using Mapbox GL JS. I wanted to call a specific function when the user click on a layer, so I added something like :

map.on('click', function(e) {.....});

and also :

map.on('click', 'layername', function(e) {.....});

Before the last Mapbox GL JS update, all was working fine. But now, it works.... sometimes but not always (with periods, like one day on two). I've seen exemples of click event listeners (Mapbox GL JS: ignore map click event if marker is clicked), whitch I don't use. But this is for markers; how to make it work for layers, or the whole map ?
Thank you in advance !

PS : Is anywhere a complete list of what to change to adapt to the new version ? Because this is not my only issue...
PS2 : Note that "mouseenter" and "mouseleave" work perfectly fine, sometimes.

Edit: Here is a piece of code :

mapboxgl.accessToken = 'pk.eyJ1IjoiY2xlbWFwYm94IiwiYSI6ImNqOHVsbjdpdDBxM2wyd3JwcnVjZGtsZmsifQ.cv3w8BmCJAy0f0YF1ZFSTA';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/clemapbox/cjj5g2pge0huj2sntqmafju85',
});

map.on('click', function (e) {
    console.log("click on map");
});

map.on('click', 'RER stops', function(e){
	console.log("click on RER stop");
});

map.on('mouseenter', 'RER stops', function () {
    map.getCanvas().style.cursor = 'pointer';
});

map.on('mouseleave', 'RER stops', function () {
    map.getCanvas().style.cursor = '';
});
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.45.0/mapbox-gl.js'></script>
        <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.45.0/mapbox-gl.css' rel='stylesheet' />
        <title>TEST click event</title>
    </head>
    <body>
        <section id='map' style="width:100%;height:500px"></section>
        <script src="TEST.js"></script>
    </body>
</html>

Note : 'RER stop' is a layer with the blue-and-white "RER" rounds when you zoom in.

4
  • stackoverflow.com/help/mcve Commented Jul 4, 2018 at 12:32
  • It's pretty hard to give you any useful information without seeing your code and exactly what you're trying to do. You need to be much more specific. Commented Jul 5, 2018 at 0:28
  • ok, I'm building a minimal code to show you (my currunt code is pretty long) Commented Jul 5, 2018 at 13:11
  • I have this exact issue, click events stopped working even though the only change was updating mapbox-gl, frustrating Commented Jul 26, 2018 at 14:44

1 Answer 1

2

Moving to MapBox GL JS version 0.48 solved this for me!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.