<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nolan Hager on Medium]]></title>
        <description><![CDATA[Stories by Nolan Hager on Medium]]></description>
        <link>https://medium.com/@nolan.hager?source=rss-353246d86c9d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*C9bueIFASmuIt2zZcZg4xw.jpeg</url>
            <title>Stories by Nolan Hager on Medium</title>
            <link>https://medium.com/@nolan.hager?source=rss-353246d86c9d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:33:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nolan.hager/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Spiderifying Clustered Layers in Mapbox-GL]]></title>
            <link>https://medium.com/@nolan.hager/spiderifying-clustered-layers-in-mapbox-gl-c4f8510a13e8?source=rss-353246d86c9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4f8510a13e8</guid>
            <category><![CDATA[tech-notes]]></category>
            <category><![CDATA[mapbox]]></category>
            <category><![CDATA[spiderify]]></category>
            <category><![CDATA[supercluster]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Nolan Hager]]></dc:creator>
            <pubDate>Mon, 25 Jun 2018 13:44:47 GMT</pubDate>
            <atom:updated>2018-07-05T18:22:02.460Z</atom:updated>
            <content:encoded><![CDATA[<p>At the time of writing, <a href="https://www.mapbox.com/">Mapbox</a>-gl provides the ability to cluster layers, but not the ability to spiderify/extract the features from those clusters. That is not to say a user can’t zoom in till those clusters break up, but sometimes that will cause too many keystrokes/mouse movements for a user. After a bit of digging, it is clear that the functionality is under the hood of mapbox-gl but not yet exposed for developers to use. There are plenty of examples of <a href="https://jsfiddle.net/aznkw784/">how to accomplish this</a>, and we are not saying this is the only way, but it is the way that worked best for us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I02ve27OCr-Jl6yO8x78Nw.png" /><figcaption>Spiderified AIS data from <a href="https://spire.com/">Spire</a> using Mapbox-GL</figcaption></figure><h3>Required Libraries</h3><ul><li><a href="https://github.com/mapbox/supercluster">supercluster</a></li><li><a href="https://github.com/bewithjonam/mapboxgl-spiderifier">mapboxgl-spiderifier</a></li></ul><h3>Basic Flow</h3><p>Let us say we are going to add a layer of all fast food restaurants in the area to our map. The steps would be as follows:</p><ol><li>Get fast food geojson data from API</li><li>Add fast food data source based on the geojson from step 1</li><li>Add fast food Layer with cluster styling</li><li>Add supercluster instance for the fast food layer with the proper zoom levels from fast food layer styling</li></ol><p>After this, there will be two instances of supercluster; one which mapbox will manage and one we will manage. As long as our version of supercluster matches mapbox’s version of supercluster, then the cluster ids will match up. This is important when extracting features from to spider on the map. This can make things tricky because both supercluster instances have to be synced but there are <a href="https://www.mapbox.com/mapbox-gl-js/api/#mapdataevent">events</a> for data source changes which make this a bit easier.</p><p><strong>NOTE:</strong> At the time of this article supercluster 2.3 is the current version used by mapbox-gl, but supercluster 3.X has been released and they may upgrade to it at any time.</p><h4>Setting up supercluster</h4><p>As mentioned before it is important that the supercluster we create has settings based off of the layer’s tile size and max zoom. The following code shows how to extract those values from the layerSource.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41de19058ee431fa83751f71c6b2b034/href">https://medium.com/media/41de19058ee431fa83751f71c6b2b034/href</a></iframe><p>It is very important to set radius, extent, maxZoom as seen in the above example. Those are the same fields that Mapbox sets when creating the supercluster object <a href="https://github.com/mapbox/mapbox-gl-js/blob/cd88621ad88d577a952ae54024b537f28659c395/src/source/geojson_source.js#L130">under the hood</a>.</p><p>The number 8192 comes from the constant in the mapbox-gl library called EXTENT. You can read more about it <a href="https://github.com/mapbox/mapbox-gl-js/blob/master/src/data/extent.js">in the mapbox source</a>. We need it in order to calculate the scale at the time so that we can make sure the cluster radius matches the tile. Another important thing to note is that maxZoom is one less than the layer max zoom. This makes sure that items appear as individual items and not clusters when at the layers max zoom level.</p><h4>Pulling data from the Clustered feature</h4><p>In order to get the data out of cluster in order to spiderify or display it, the following steps are taken:</p><ol><li>Cluster on the map is clicked</li><li>Layer click event returns a feature which represents a supercluster which contains thecluster_id</li><li>Use the cluster_id to query the supercluster instances that relate to the layer</li><li>Finally, once we have the features from the system, we can either pass them off to the spiderify library or process the features as needed</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daeb1bdfc75180ae65ec8fb07047a300/href">https://medium.com/media/daeb1bdfc75180ae65ec8fb07047a300/href</a></iframe><p><strong>NOTE:</strong> Remember this is an older version of super cluster where getLeaves() needs a zoom level</p><h4>Spidering the data on the map</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce0099d6d84ccf51ab0339c3e9e29a30/href">https://medium.com/media/ce0099d6d84ccf51ab0339c3e9e29a30/href</a></iframe><p><strong>NOTE:</strong> One thing to keep in mind is that the location provided by the event is where the user clicked on on the map and not the exact location of the feature. If you want that, then extract that out of the selected feature.</p><h4>Finally, Unspidering the data</h4><p>The logic for unspidering the data is broken up into to functions to make it a little easier to follow and implement. There is a close method and an unspider method. The close method detects if the event has spiderify properties and locates the related map container. If the event is related to spidered data, then the map container is passed to the unspiderify method. That method removes the spiderify markers then turns off the close spider map event.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44d20963f6ebc0cbf03116b2b7f7fb20/href">https://medium.com/media/44d20963f6ebc0cbf03116b2b7f7fb20/href</a></iframe><h3>Resources</h3><p><a href="https://github.com/mapbox/mapbox-gl-js/blob/cd88621ad88d577a952ae54024b537f28659c395/src/source/geojson_source.js#L130">GitHub — mapbox-gl-js/geojson_source.js · Setting Supercluster configuration</a><br><a href="https://github.com/mapbox/mapbox-gl-js/blob/master/src/data/extent.js">GitHub — mapbox-gl-js/extent.js · Definition of EXTENT</a><br><a href="https://github.com/mapbox/supercluster">GitHub — mapbox/supercluster: A crazy fast geospatial point clustering library for browsers and Node.</a><br><a href="https://github.com/bewithjonam/mapboxgl-spiderifier">GitHub — bewithjonam/mapboxgl-spiderifier: Spiderify markers on mapbox-gl</a><br><a href="https://github.com/mapbox/mapbox-gl-js/issues/3318">GitHub — See features in cluster · Issue #3318 · mapbox/mapbox-gl-js</a><br><a href="https://github.com/mapbox/mapbox-gl-js/issues/2661">GitHub — Click on cluster points to get all contained points properties · Issue #2661 · mapbox/mapbox-gl-js</a><br><a href="https://jsfiddle.net/aznkw784/">JSFiddle — Another way to pull features from a cluster · barrylachapelle </a><br><a href="https://www.mapbox.com/mapbox-gl-js/api/#mapdataevent">Mapbox GL JS API Documentation about </a><a href="https://www.mapbox.com/mapbox-gl-js/api/#mapdataevent">mapdataevent</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4f8510a13e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>