@@ -7,9 +7,12 @@ const map = new mapboxgl.Map({
77 zoom : 3 ,
88} ) ;
99
10+ // Add zoom and rotation controls to the map.
1011map . addControl ( new mapboxgl . NavigationControl ( ) ) ;
1112
1213map . on ( 'load' , function ( ) {
14+ // Add a new source from our GeoJSON data and set the
15+ // 'cluster' option to true. GL-JS will add the point_count property to your source data.
1316 map . addSource ( 'garages' , {
1417 type : 'geojson' ,
1518 data : garages ,
@@ -18,11 +21,13 @@ map.on('load', function () {
1821 clusterRadius : 50 ,
1922 } ) ;
2023
24+ // Add a cluster layer. This layer will cluster all point features in the source layer
2125 map . addLayer ( {
2226 id : 'clusters' ,
2327 type : 'circle' ,
2428 source : 'garages' ,
2529 filter : [ 'has' , 'point_count' ] ,
30+ // cluster style configurations
2631 paint : {
2732 'circle-color' : [
2833 'step' ,
@@ -45,6 +50,7 @@ map.on('load', function () {
4550 } ,
4651 } ) ;
4752
53+ // Add a layer for the clusters' count labels
4854 map . addLayer ( {
4955 id : 'cluster-count' ,
5056 type : 'symbol' ,
@@ -57,6 +63,7 @@ map.on('load', function () {
5763 } ,
5864 } ) ;
5965
66+ // Add a layer for the unclustered point features.
6067 map . addLayer ( {
6168 id : 'unclustered-point' ,
6269 type : 'circle' ,
@@ -69,6 +76,8 @@ map.on('load', function () {
6976 'circle-stroke-color' : '#fff' ,
7077 } ,
7178 } ) ;
79+
80+ // inspect a cluster on click on clustered point
7281 map . on ( 'click' , 'clusters' , function ( e ) {
7382 const features = map . queryRenderedFeatures ( e . point , {
7483 layers : [ 'clusters' ] ,
@@ -86,19 +95,23 @@ map.on('load', function () {
8695 }
8796 ) ;
8897 } ) ;
98+
99+ // inspect a cluster on click on an unclustered point
89100 map . on ( 'click' , 'unclustered-point' , function ( e ) {
90101 const { popUpMarkup } = e . features [ 0 ] . properties ;
91102 const coordinates = e . features [ 0 ] . geometry . coordinates . slice ( ) ;
92103 while ( Math . abs ( e . lngLat . lng - coordinates [ 0 ] ) > 180 ) {
93104 coordinates [ 0 ] += e . lngLat . lng > coordinates [ 0 ] ? 360 : - 360 ;
94105 }
95106
107+ // Populate the popup and set its coordinates
96108 new mapboxgl . Popup ( )
97109 . setLngLat ( coordinates )
98110 . setHTML ( popUpMarkup )
99111 . addTo ( map ) ;
100112 } ) ;
101113
114+ // Change the cursor to a pointer when the mouse is over the places layer.
102115 map . on ( 'mouseenter' , 'clusters' , function ( ) {
103116 map . getCanvas ( ) . style . cursor = 'pointer' ;
104117 } ) ;
0 commit comments