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

Create and style clusters

Use Mapbox GL JS' built-in functions to visualize points as clusters.

<!DOCTYPE html>
<meta charset="utf-8" />
<title>Create and style clusters</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src=""></script>
<link href="" rel="stylesheet" />
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
<div id="map"></div>
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v10',
center: [-103.59179687498357, 40.66995747013945],
zoom: 3
map.on('load', function () {
// Add a new source from our GeoJSON data and
// set the 'cluster' option to true. GL-JS will
// add the point_count property to your source data.
map.addSource('earthquakes', {
type: 'geojson',
// Point to GeoJSON data. This example visualizes all M1.0+ earthquakes
// from 12/22/15 to 1/21/16 as logged by USGS' Earthquake hazards program.
cluster: true,
clusterMaxZoom: 14, // Max zoom to cluster points on
clusterRadius: 50 // Radius of each cluster when clustering points (defaults to 50)
id: 'clusters',
type: 'circle',
source: 'earthquakes',
filter: ['has', 'point_count'],
paint: {
// Use step expressions (
// with three steps to implement three types of circles:
// * Blue, 20px circles when point count is less than 100
// * Yellow, 30px circles when point count is between 100 and 750
// * Pink, 40px circles when point count is greater than or equal to 750
'circle-color': [
['get', 'point_count'],
'circle-radius': [
['get', 'point_count'],
id: 'cluster-count',
type: 'symbol',
source: 'earthquakes',
filter: ['has', 'point_count'],
layout: {
'text-field': '{point_count_abbreviated}',
'text-font': ['DIN Offc Pro Medium', 'Arial Unicode MS Bold'],
'text-size': 12
id: 'unclustered-point',
type: 'circle',
source: 'earthquakes',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#11b4da',
'circle-radius': 4,
'circle-stroke-width': 1,
'circle-stroke-color': '#fff'
// inspect a cluster on click
map.on('click', 'clusters', function (e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['clusters']
var clusterId = features[0].properties.cluster_id;
function (err, zoom) {
if (err) return;
center: features[0].geometry.coordinates,
zoom: zoom
// When a click event occurs on a feature in
// the unclustered-point layer, open a popup at
// the location of the feature, with
// description HTML from its properties.
map.on('click', 'unclustered-point', function (e) {
var coordinates = e.features[0].geometry.coordinates.slice();
var mag = e.features[0].properties.mag;
var tsunami;
if (e.features[0].properties.tsunami === 1) {
tsunami = 'yes';
} else {
tsunami = 'no';
// Ensure that if the map is zoomed out such that
// multiple copies of the feature are visible, the
// popup appears over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
new mapboxgl.Popup()
'magnitude: ' + mag + '<br>Was there a tsunami?: ' + tsunami
map.on('mouseenter', 'clusters', function () {
map.getCanvas().style.cursor = 'pointer';
map.on('mouseleave', 'clusters', function () {
map.getCanvas().style.cursor = '';
Was this example helpful?