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.8.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.8.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>
This code snippet will not work as expected until you replace
YOUR_MAPBOX_ACCESS_TOKEN
with an access token from your Mapbox account.import React, { useEffect, useRef } from 'react';
import mapboxgl from 'mapbox-gl';
import mapboxglCompare from 'mapbox-gl-compare';
import 'mapbox-gl/dist/mapbox-gl.css';
import 'mapbox-gl-compare/dist/mapbox-gl-compare.css';
// extend mapboxgl, adding the Compare class
mapboxgl.Compare = mapboxglCompare;
const mapStyle = { position: 'absolute', top: 0, bottom: 0, width: '100%' };
const MapboxExample = () => {
const mapRef = useRef();
const beforeMapContainerRef = useRef();
const afterMapContainerRef = useRef();
const comparisonContainerRef = useRef();
useEffect(() => {
// some development servers will run this hook more than once
// return if the map has already been initialized
if (mapRef.current) return;
// 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: beforeMapContainerRef.current,
style: 'mapbox://styles/mapbox/streets-v12',
center: [-77.01866, 38.888],
pitch: 75,
zoom: 15
});
const afterMap = new mapboxgl.Map({
container: afterMapContainerRef.current,
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);
const updatedFilter = [
'all',
currFilter,
[
'case',
['<=', ['pitch'], 60],
true,
['all', ['<=', ['distance-from-center'], 2], ['>', ['pitch'], 60]],
true,
false
]
];
afterMap.setFilter(layerId, updatedFilter);
}
});
mapRef.current = new mapboxgl.Compare(
beforeMap,
afterMap,
comparisonContainerRef.current
);
}, []);
return (
<div
id="comparison-container"
ref={comparisonContainerRef}
style={{ height: '100%' }}
>
<div id="before" ref={beforeMapContainerRef} style={mapStyle}></div>
<div id="after" ref={afterMapContainerRef} style={mapStyle}></div>
</div>
);
};
export default MapboxExample;
This code snippet will not work as expected until you replace
YOUR_MAPBOX_ACCESS_TOKEN
with an access token from your Mapbox account.Was this example helpful?