Examples
Styles (11)Add a default markerAdd an animated icon to the mapAdd a generated icon to the mapGenerate and add a missing icon to the mapAdd a stretchable image to the mapAdd an icon to the mapDisplay a map with a custom styleRender world copiesDisplay a satellite mapChange a map's styleDisplay a mapLayers (32)Extrude polygons for 3D indoor mappingDisplay buildings in 3DAdd a 3D modelAdjust a layer's opacityAnimate a lineAnimate a series of imagesAnimate a pointChange building color based on zoom levelChange the case of labelsDisplay HTML clusters with custom propertiesCreate and style clustersChange a layer's color with buttonsAdd a custom style layerStyle circles with a data-driven propertyStyle lines with a data-driven propertyDisplay and style rich text labelsAdd a pattern to a polygonAdd a new layer below labelsDraw GeoJSON pointsAdd a GeoJSON lineAdd a GeoJSON polygonCreate a heatmap layerAdd hillshadingCreate a gradient line using an expressionAdd multiple geometries from one GeoJSON sourceUse a fallback imageStyle ocean depth dataShow and hide layersChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelVariable label placementVisualize population densitySources (10)Add a canvas sourceJoin local JSON data with vector tile geometriesAdd an imageAdd live realtime dataUpdate a feature in realtimeAdd a raster tile sourceAdd a third party vector tile sourceAdd a vector tile sourceAdd a videoAdd a WMS sourceUser interaction (21)Customize camera animationsAnimate 3D buildings based on ambient soundsDisable map rotationCreate a draggable MarkerFilter features within map viewFilter symbols by text inputFilter symbols by toggling a listCreate a hover effectDisplay a non-interactive mapChange a map's languageMeasure distancesGet coordinates of the mouse pointerHighlight features containing similar dataSelect features around a clicked pointGet features under the mouse pointerRestrict map panning to an areaCreate a time sliderToggle interactionsHighlight features within a bounding boxFit to the bounds of a LineStringCreate a draggable pointCamera (12)Animate map camera around a pointAnimate a point along a routeCenter the map on a clicked symbolFit a map to a bounding boxFly to a locationSlowly fly to a locationNavigate the map with game-like controlsJump to a series of locationsOffset the vanishing point using paddingPlay map locations as a slideshowFly to a location based on scroll positionSet pitch and bearingControls and overlays (17)Animate a markerChange the default position for attributionAdd custom icons with MarkersDisable scroll zoomView a fullscreen mapLocate the userSwipe between mapsDisplay driving directionsShow drawn polygon areaAdd a geocoderAdd a marker using a place nameDisplay map navigation controlsShow polygon information on clickDisplay a popup on clickDisplay a popup on hoverDisplay a popupAttach a popup to a marker instanceGeocoder (9)Supplement forward geocoding search results from another data sourceAccept coordinates as input to a geocoderLimit geocoder results to a named regionUse a custom render function with a geocoderPlace the geocoder input outside the mapUse the geocoder without a mapUse a custom camera animation with a geocoderLocalize the geocoder to a given languageSet a point after Geocoder resultBrowser support (1)Check if Mapbox GL JS is supportedInternationalization support (2)Use locally generated ideographsAdd support for right-to-left scripts

Update a choropleth layer by zoom level

Using 2014 census data, display state or county population depending on zoom level.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Update a choropleth layer by zoom level</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.legend {
background-color: #fff;
border-radius: 3px;
bottom: 30px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
padding: 10px;
position: absolute;
right: 10px;
z-index: 1;
}
.legend h4 {
margin: 0 0 10px;
}
.legend div span {
border-radius: 50%;
display: inline-block;
height: 10px;
margin-right: 5px;
width: 10px;
}
</style>
<div id="map"></div>
<div id="state-legend" class="legend">
<h4>Population</h4>
<div><span style="background-color: #723122"></span>25,000,000</div>
<div><span style="background-color: #8B4225"></span>10,000,000</div>
<div><span style="background-color: #A25626"></span>7,500,000</div>
<div><span style="background-color: #B86B25"></span>5,000,000</div>
<div><span style="background-color: #CA8323"></span>2,500,000</div>
<div><span style="background-color: #DA9C20"></span>1,000,000</div>
<div><span style="background-color: #E6B71E"></span>750,000</div>
<div><span style="background-color: #EED322"></span>500,000</div>
<div><span style="background-color: #F2F12D"></span>0</div>
</div>
<div id="county-legend" class="legend" style="display: none;">
<h4>Population</h4>
<div><span style="background-color: #723122"></span>1,000,000</div>
<div><span style="background-color: #8B4225"></span>500,000</div>
<div><span style="background-color: #A25626"></span>100,000</div>
<div><span style="background-color: #B86B25"></span>50,000</div>
<div><span style="background-color: #CA8323"></span>10,000</div>
<div><span style="background-color: #DA9C20"></span>5,000</div>
<div><span style="background-color: #E6B71E"></span>1,000</div>
<div><span style="background-color: #EED322"></span>100</div>
<div><span style="background-color: #F2F12D"></span>0</div>
</div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [-98, 38.88],
minZoom: 3,
zoom: 3
});
var zoomThreshold = 4;
map.on('load', function() {
map.addSource('population', {
'type': 'vector',
'url': 'mapbox://mapbox.660ui7x6'
});
map.addLayer(
{
'id': 'state-population',
'source': 'population',
'source-layer': 'state_county_population_2014_cen',
'maxzoom': zoomThreshold,
'type': 'fill',
'filter': ['==', 'isState', true],
'paint': {
'fill-color': [
'interpolate',
['linear'],
['get', 'population'],
0,
'#F2F12D',
500000,
'#EED322',
750000,
'#E6B71E',
1000000,
'#DA9C20',
2500000,
'#CA8323',
5000000,
'#B86B25',
7500000,
'#A25626',
10000000,
'#8B4225',
25000000,
'#723122'
],
'fill-opacity': 0.75
}
},
'waterway-label'
);
map.addLayer(
{
'id': 'county-population',
'source': 'population',
'source-layer': 'state_county_population_2014_cen',
'minzoom': zoomThreshold,
'type': 'fill',
'filter': ['==', 'isCounty', true],
'paint': {
'fill-color': [
'interpolate',
['linear'],
['get', 'population'],
0,
'#F2F12D',
100,
'#EED322',
1000,
'#E6B71E',
5000,
'#DA9C20',
10000,
'#CA8323',
50000,
'#B86B25',
100000,
'#A25626',
500000,
'#8B4225',
1000000,
'#723122'
],
'fill-opacity': 0.75
}
},
'waterway-label'
);
});
var stateLegendEl = document.getElementById('state-legend');
var countyLegendEl = document.getElementById('county-legend');
map.on('zoom', function() {
if (map.getZoom() > zoomThreshold) {
stateLegendEl.style.display = 'none';
countyLegendEl.style.display = 'block';
} else {
stateLegendEl.style.display = 'block';
countyLegendEl.style.display = 'none';
}
});
</script>
</body>
</html>
Was this page helpful?