Skip to content

Commit

Permalink
mod: map is now (re)populated with markers on style change rather tha…
Browse files Browse the repository at this point in the history
…n on load: this is more general and would allow to retain (actually readd) markers when changing map style during project editing (currently, map.setStyle(.) removes markers... mapbox/mapbox-gl-js#4006)
  • Loading branch information
axyorah committed Feb 25, 2021
1 parent 46ad743 commit 73080f9
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions public/js/mapSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const getAreaSource = (area) => {
const getAreaLayer = (area) => {
return {
'id': `cities-${area.name}`,
"interactive": true,
'type': 'circle',
'source': `cities-${area.name}`,
'paint': {
Expand Down Expand Up @@ -68,20 +69,21 @@ const removePopupCallback = (popup) => {
}
}

const showMarkers = () => {
const populateMap = () => {
for (let area of areas) {
if ( !map.getSource(`cities-${area.name}`) ) {
// add city-data of the current area in correct format
map.addSource(`cities-${area.name}`, getAreaSource(area));

// add a layer showing the city markers for the current area
map.addLayer(getAreaLayer(area));

// add city-data of the current area in correct format
map.addSource(`cities-${area.name}`, getAreaSource(area));

// add a layer showing the city markers for the current area
map.addLayer(getAreaLayer(area));

// show popup on mouseenter, remove on mouseleave
let popup = new mapboxgl.Popup();
map.on('mouseenter', `cities-${area.name}`, addPopupCallback(popup));
map.on('mouseleave', `cities-${area.name}`, removePopupCallback(popup));
// show popup on mouseenter, remove on mouseleave
let popup = new mapboxgl.Popup();
map.on('mouseenter', `cities-${area.name}`, addPopupCallback(popup));
map.on('mouseleave', `cities-${area.name}`, removePopupCallback(popup));
}
}
}

map.on('load', showMarkers);
map.on("styledata", populateMap);

0 comments on commit 73080f9

Please sign in to comment.