Getting started (1)Display a mapStyles (11)Add an animated icon to the mapAdd a default markerGenerate and add a missing icon to the mapAdd a generated icon to the mapAdd an icon to the mapAdd a stretchable image to the mapDisplay a map with a custom styleRender world copiesChange a map's styleDisplay a mapDisplay a satellite mapLayers (40)Display buildings in 3DAdd a 3D modelExtrude polygons for 3D indoor mappingAdd an animated icon to the mapGenerate and add a missing icon to the mapAdd a generated icon to the mapAdd an icon to the mapAdd a stretchable image to the mapAdjust 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 lines with a data-driven propertyStyle circles with a data-driven propertyDisplay and style rich text labelsUse a fallback imageAdd a pattern to a polygonAdd a GeoJSON lineAdd a new layer below labelsDraw GeoJSON pointsAdd a GeoJSON polygonCreate a heatmap layerAdd hillshadingAdd an imageDisplay line that crosses 180th meridianCreate a gradient line using an expressionAdd multiple geometries from one GeoJSON sourceStyle ocean depth dataShow and hide layersChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelVariable label placementAdd a videoVisualize population densitySources (16)Animate a lineAnimate a series of imagesAnimate a pointAdd a canvas sourceJoin local JSON data with vector tile geometriesAdd hillshadingAdd an imageDisplay line that crosses 180th meridianAdd live realtime dataUpdate a feature in realtimeAdd a raster tile sourceAdd a third party vector tile sourceCreate a time sliderAdd a vector tile sourceAdd a videoAdd a WMS sourceUser interaction (27)Adjust a layer's opacityCustomize camera animationsCenter the map on a clicked symbolChange a layer's color with buttonsAnimate 3D buildings based on ambient soundsDisable map rotationCreate a draggable MarkerFilter features within map viewFilter symbols by toggling a listFilter symbols by text inputCreate 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 areaChange a map's styleCreate a time sliderToggle interactionsChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelHighlight features within a bounding boxFit to the bounds of a LineStringCreate a draggable pointCamera (13)Animate map camera around a pointAnimate a point along a routeCustomize camera animationsCenter the map on a clicked symbolSlowly fly to a locationFit a map to a bounding boxFly to a locationNavigate the map with game-like controlsJump to a series of locationsPlay map locations as a slideshowOffset the vanishing point using paddingFly 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 (10)Supplement forward geocoding search results from another data sourceAccept coordinates as input to a geocoderUse a custom render function with a geocoderLimit geocoder results to a named regionUse the geocoder without a mapPlace the geocoder input outside the mapUse a custom camera animation with a geocoderLocalize the geocoder to a given languageAdd a geocoderSet 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

Join local JSON data with vector tile geometries

Style a choropleth by merging local JSON data with vector tile geometries.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Join local JSON data with vector tile geometries</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></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: [-99.9, 41.5],
zoom: 1
});
// Join local JSON data with vector tile geometry
// USA unemployment rate in 2009
// Source https://data.bls.gov/timeseries/LNS14000000
var maxValue = 13;
var data = [
{ 'STATE_ID': '01', 'unemployment': 13.17 },
{ 'STATE_ID': '02', 'unemployment': 9.5 },
{ 'STATE_ID': '04', 'unemployment': 12.15 },
{ 'STATE_ID': '05', 'unemployment': 8.99 },
{ 'STATE_ID': '06', 'unemployment': 11.83 },
{ 'STATE_ID': '08', 'unemployment': 7.52 },
{ 'STATE_ID': '09', 'unemployment': 6.44 },
{ 'STATE_ID': '10', 'unemployment': 5.17 },
{ 'STATE_ID': '12', 'unemployment': 9.67 },
{ 'STATE_ID': '13', 'unemployment': 10.64 },
{ 'STATE_ID': '15', 'unemployment': 12.38 },
{ 'STATE_ID': '16', 'unemployment': 10.13 },
{ 'STATE_ID': '17', 'unemployment': 9.58 },
{ 'STATE_ID': '18', 'unemployment': 10.63 },
{ 'STATE_ID': '19', 'unemployment': 8.09 },
{ 'STATE_ID': '20', 'unemployment': 5.93 },
{ 'STATE_ID': '21', 'unemployment': 9.86 },
{ 'STATE_ID': '22', 'unemployment': 9.81 },
{ 'STATE_ID': '23', 'unemployment': 7.82 },
{ 'STATE_ID': '24', 'unemployment': 8.35 },
{ 'STATE_ID': '25', 'unemployment': 9.1 },
{ 'STATE_ID': '26', 'unemployment': 10.69 },
{ 'STATE_ID': '27', 'unemployment': 11.53 },
{ 'STATE_ID': '28', 'unemployment': 9.29 },
{ 'STATE_ID': '29', 'unemployment': 9.94 },
{ 'STATE_ID': '30', 'unemployment': 9.29 },
{ 'STATE_ID': '31', 'unemployment': 5.45 },
{ 'STATE_ID': '32', 'unemployment': 4.21 },
{ 'STATE_ID': '33', 'unemployment': 4.27 },
{ 'STATE_ID': '34', 'unemployment': 4.09 },
{ 'STATE_ID': '35', 'unemployment': 7.83 },
{ 'STATE_ID': '36', 'unemployment': 8.01 },
{ 'STATE_ID': '37', 'unemployment': 9.34 },
{ 'STATE_ID': '38', 'unemployment': 11.23 },
{ 'STATE_ID': '39', 'unemployment': 7.08 },
{ 'STATE_ID': '40', 'unemployment': 11.22 },
{ 'STATE_ID': '41', 'unemployment': 6.2 },
{ 'STATE_ID': '42', 'unemployment': 9.11 },
{ 'STATE_ID': '44', 'unemployment': 10.42 },
{ 'STATE_ID': '45', 'unemployment': 8.89 },
{ 'STATE_ID': '46', 'unemployment': 11.03 },
{ 'STATE_ID': '47', 'unemployment': 7.35 },
{ 'STATE_ID': '48', 'unemployment': 8.92 },
{ 'STATE_ID': '49', 'unemployment': 7.65 },
{ 'STATE_ID': '50', 'unemployment': 8.01 },
{ 'STATE_ID': '51', 'unemployment': 7.62 },
{ 'STATE_ID': '53', 'unemployment': 7.77 },
{ 'STATE_ID': '54', 'unemployment': 8.49 },
{ 'STATE_ID': '55', 'unemployment': 9.42 },
{ 'STATE_ID': '56', 'unemployment': 7.59 }
];
map.on('load', function () {
// Add source for state polygons hosted on Mapbox, based on US Census Data:
// https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
map.addSource('states', {
type: 'vector',
url: 'mapbox://mapbox.us_census_states_2015'
});
var expression = ['match', ['get', 'STATE_ID']];
// Calculate color for each state based on the unemployment rate
data.forEach(function (row) {
var green = (row['unemployment'] / maxValue) * 255;
var color = 'rgba(' + 0 + ', ' + green + ', ' + 0 + ', 1)';
expression.push(row['STATE_ID'], color);
});
// Last value is the default, used where there is no data
expression.push('rgba(0,0,0,0)');
// Add layer from the vector tile source with data-driven style
map.addLayer(
{
'id': 'states-join',
'type': 'fill',
'source': 'states',
'source-layer': 'states',
'paint': {
'fill-color': expression
}
},
'waterway-label'
);
});
</script>
</body>
</html>
Was this example helpful?