Mapbox - display data on a cloropleth map

Hey there,

I am trying to follow a tutorial on Mapbox’s website to create a cloropleth map using Mapbox. I have successfully managed follow the steps in Part 1 of the tutorial. However I am unable to follow the steps in the Part 2. I can get the mapbox style to load on the map (image attached). But I am unable to bring any interactive in the map ie. when the user hovers over the specific state on the map, it should display the data properties. I have tried multiple ways to update the source and layer information but havent succeeded. Any help would be much appreciated. Thanks.

Part 1 - Make a choropleth map, part 1: create a style | Help | Mapbox
Part 2 - Make a choropleth map, part 2: add interactivity | Help | Mapbox

Hi, the step 2 will require you a little bit of code.

Everything in purple/pink is what is already handled by your map element, everything in green and blue seems to be done through custom code in their demo.

You will have to put yourself two box on absolute position to display the data, like they did. But in WeWeb you’re able to do it in nocode. You will also create variable that will store the data and use them inside your containers to display them.

The blue box is fixed, nothing dynamic, you can create it fully with nocode yourself.

The green box is the one requiring a little bit of javascript to handle the hover and putting the data inside the variables that will then be displayed.

From the documentation, you need to do this on map load.

map.on('mousemove', (event) => {
const states = map.queryRenderedFeatures(event.point, {
layers: ['statedata']
});
document.getElementById('pd').innerHTML = states.length
? `<h3>${states[0].properties.name}</h3><p><strong><em>${states[0].properties.density}</strong> people per square mile</em></p>`
: `<p>Hover over a state!</p>`;
});

Their code is made to update directly the html div, I adapted the code so it update the variable you’re using to display your data instead.

const map = <your-mapbox-instance>
map.on('mousemove', (event) => {
  const states = map.queryRenderedFeatures(event.point, { layers: ['statedata'] });
  wwLib.wwVariable.updateValue('your-var-id', states[0].properties);
});

So, what you can try here is to

  1. Create a variable (type object) to store the hovered state
  2. Ciick on your map and add a workflow on map load with a js action
  3. Put this code inside your action, you will have to replace two part
    a. <your-mapbox-instance> by clicking on the mapbox instance on your data

b. <your-var-id> by clicking on the copy ID button on your variable

The state variable should the contain a state with his properties like name and density you will have to display inside your green box :slight_smile:

Hello @Alexis ,

I’m currently facing an issue where I’m unable to interact with map events. I have a simple piece of code that I’m using for testing purposes, but it doesn’t seem to work as expected. Here’s what I have so far:

[ON MAP LOAD]

const map = variables[ /* Mapbox - instance */ ‘9994f56e-a40a-4a5f-a1bc-e2ce0d03c8c5-instance’];

map.on(‘click’, (e) => {
alert(“test”);
console.log(e);
});

When I click on the map, I expect an alert saying “test” and a log of the event object in the console. However, nothing happens when I click on the map. I’m wondering if I might be missing something in my approach or if there’s an issue with how I’m accessing the Mapbox instance.

Any insights or suggestions would be greatly appreciated!

Thanks in advance!

Hi, I’m sorry for the late response, it may be a bug with the map instance, please create a support ticket on support.weweb.io and we will try to give a look :slight_smile:

1 Like