How to create Cluster, marker annotation and popover using Mapbox
Using Mapbox there are many scenario when we need to show a huge data set on the map. Markers will overlap each other, and the map will look very cluttered. Data clustering is one of the best options to represents large data on the map and user experience will be high as well.
Clustering data based on map cameras zoom level is a good way to provide user with a cleaner UI experience and less overwhelming location data experience.
I have created a cluster-based Map experience using the COVID-19 data set, and the same I am going to write, How I did it. Before jumping into cluster and marker, if you are interested to understand Map layer and data set preparation using geoJSON, I would suggest going through my previous blog. Also for Cluster experience, have a look at the live COVID-19 data showing on Map.
- Mapbox Layer creation Blog - https://lazypandatech.com/blog/Mapbox/14/Create-COVID-19-tracker-using-Mapbox-and-Angular-9
- Cluster experience using COVID-19 data - https://lazypandatech.com/covid-19
The following items are going to cover in this blog -
- Data set creation using Python script and transform to geoJSON
- Initializing Cluster layer using SuperCluster module in mapbox-gl
- Load and configure data on a map based on map camera zoom level
- Show annotation and popover on the map
Data set creation using Python script and transform to geoJSON
To create Cluster data, first we need a JSON data in geoJSON format, and my targetted JSON will look like following -
To get the data source, I have used https://github.com/microsoft/Bing-COVID-19-Data/tree/master/data to get the CSV file. Then I ran a Python script to get CSV data into a JSON file based on my needs. Here the below is the python script (CSV to JSON using HTTPS connection) with pandas and request library to get the desire data -
Once I got the country-wise-covid.json file, I turn it into a geoJSON file -
Initializing Cluster layer using SuperCluster module in mapbox-gl
Now next step is to use the geoJSON to cluster and initialize it with minZomm, maxZoom, radius, and the number you wanted to on top of the cluster circle. For to get the nearest locations confirmed cases count, I have used to map and reduce function to get the accumulated numbers.
Then update cluster data based on world boundaries or countries' boundaries, depends upon your needs. (In my case it is word boundary) Also, set the cluster layer and data source for the Cluster as well.
To handle cluster click event on map, need to add click event on map cluster layer and based on zoom level, find nearest cluster and show next child. If any of parent cluster node does not have any clild node, that means its a point and show marker with pop over.
Show annotation and popover on the map
To show marker on the map, and popover on to of the marker, you can use the following code -
And that's it. You can execute the application if your data set is correctly configured, cluster data should have appeared without any error.
- Lazy Panda Tech