Skip to main content

Filter symbols based on pitch and distance

Use ['pitch'] and ['distance-from-center'] expressions in the filter field of a symbol layer to remove large size POI labels in the far distance at high pitch, freeing up that screen real-estate for smaller road and street labels.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Filter symbols based on pitch and distance</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
body {
overflow: hidden;
}

body * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

.map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-compare/v0.4.0/mapbox-gl-compare.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-compare/v0.4.0/mapbox-gl-compare.css" type="text/css">
<div id="comparison-container">
<div id="before" class="map"></div>
<div id="after" class="map"></div>
</div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const beforeMap = new mapboxgl.Map({
container: 'before',
style: 'mapbox://styles/mapbox/streets-v12',
center: [-77.01866, 38.888],
pitch: 75,
zoom: 15
});

const afterMap = new mapboxgl.Map({
container: 'after',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/streets-v12',
center: [-77.01866, 38.888],
pitch: 75,
zoom: 15
});

afterMap.once('load', () => {
const poiLayers = ['poi-label', 'transit-label'];

for (const layerId of poiLayers) {
const currFilter = afterMap.getFilter(layerId);
// Add in an additional condition for filtering based on ["pitch"] and ["distance-from-center"]
const updatedFilter = [
'all',
currFilter,
[
'case',
// Always show the symbol when pitch <= 60
['<=', ['pitch'], 60],
true,
// When pitch > 60, show the symbol only when it is close to the camera ( distance <= 2 )
[
'all',
['<=', ['distance-from-center'], 2],
['>', ['pitch'], 60]
],
true,
// Hide in the remaining case, far and high pitch
false
]
];

afterMap.setFilter(layerId, updatedFilter);
}
});
const map = new mapboxgl.Compare(
beforeMap,
afterMap,
'#comparison-container'
);
</script>

</body>
</html>
Was this example helpful?