For the past few months, I have been charged with visualizing data onto maps using either Google Maps or Mapbox. I chose the latter. After days and nights of struggle, I am pretty close to the finishing line and have gained quite a bit of experience in Mapbox that I want to share.
Long story short, to map data onto maps, you need to structure data into a specific structure called GEOJSON. It looks like this:

You can put anything in the “properties” key, but the rest essentially have to follow the above format. The coordinates will be used to locate markers or data on the map.
Let’s say the data that I want to map has 6 different mission areas (see the screenshot above) and my job is to map them onto the map in 6 different colors.

I have an array that contains 6 different mission areas like this and 6 different colors representing those areas
var missionarea = [area1, area2, area3, area4, area5, area6]
var colorcode = [color1, color2, color3, color4, color5, color6]
One layer approach
#create a map canvas
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [-96.797885, 39.363438],
// initial zoom
zoom: 3.3
});
#load data and create the layer
map.on("load", function() {
map.addSource('communityData', { #name the data as communityData
type: 'geojson',
data: communityData, #this is the name of your GEOJSON
});
map.addLayer({ #add the layer
"id": "commMap", #your single layer's name
"type": "circle", #meaning that each item will be a dot
"source": "communityData", #the name of your data assigned above
'layout': {},
'paint': {
"circle-radius": 8,
"circle-opacity": 1,
"circle-color": {
"property": "Mission Area",
"type": 'categorical',
"stops": [ #assign color to respective mission areas
[Missionarea[0], colorcode[0]],
[Missionarea[1], colorcode[1]],
[Missionarea[2], colorcode[2]],
[Missionarea[3], colorcode[3]],
[Missionarea[4], colorcode[4]],
[Missionarea[5], colorcode[5]],
]
}
}
});
}
Multiple Layers
In this example, I’ll label each map layer as “show0”, “show1″…”show5”
var showlist = [] //the array of layerIDs, used to categorize layers
var base = "show"
for (var i = 0; i < Missionarea.length; i++) { //populate showlist array
var text = base + i
showlist.push(text)
}
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
center: [-95.957309, 41.276479],
// initial zoom
zoom: 6
});
map.on("load", function() {
map.addSource('communityData', {
type: 'geojson',
data: communityData,
});
//*********************************** Load partners *****************************************************
communityData.features.forEach(function(feature) {
var primary = feature.properties["Mission Area"];
// iterate through the mission area array
for (var i = 0; i < missionarea.length; i++) {
if (primary == missionarea[i]) {
// assign a name to each layer
layerID = showlist[i];
if (!map.getLayer(layerID)) {
map.addLayer({
"id": layerID, #layer's name
"type": "circle",
"source": "communityData",
"paint": {
"circle-radius": 8,
"circle-opacity": 1,
"circle-color": colorcode[i], #color
},
"filter": ["all", ["==", "Mission Area", primary]]
})
}
}
}
});
Which approach is better?
If the intention is just to show the dots, there is no difference and it depends on personal preference. However, if your code gets more complicated and as in my case, I had to create at least 6 filters on the map, things will get messy and one approach will no longer allow you to do what you want. Unfortunately, I don’t have that much experience yet to tell you more and I personally believe it’s a case-by-case thing.
One thought on “Mapbox – Two ways to show a map”