Get startedGet started for free

Pieces of Flair

1. Pieces of Flair

To close out Chapter 3, we'll add a some flair to our leaflet map. We'll start by customizing the search function from the leaflet extras package to search for specific colleges we have on our map. Then we'll explore an alternate approach to presenting a large number of points on a leaflet map, called clustering.

2. The College Search

At the start of Chapter 3, we found the world's largest ball of twine and Carhenge using the addSearchOSM() function from the leaflet extras package. We can take a slightly different approach to allow users to search for specific data on our map. Let's take one last look at the California colleges example to see how this works. First, we need a subset of the IPEDS data that contains only public colleges in California. Then we pipe this data into our leaflet() call, we'll add the Esri map tile, and plot the colleges using circle markers. Then we pipe all of this into the addSearchFeatures() function and tell the function that we want to search for elements on the map in the group called "public".

3. The College Search

We will see a familiar magnifying glass icon, but now when we type in the box it will filter the data elements in the public group. For example, if we want to focus our map on UCLA, we can start to type in the name of the university and then select it from the menu. When we do this, the map will automatically zoom and center on UCLA and the marker that represents the university will be circled. We can specify the zoom level using the options argument.

4. Clustering Colleges

We have tried to address the large number of points on our map by adding the colleges in different layers. This approach has helped make the map more user-friendly, but there are still an overwhelming number of points on the map at times. An alternative solution is to cluster the colleges using the cluster options argument to the addCircleMarkers() function. By default, the clusters are orange, yellow, and green. The colors correspond to the number of the colleges represented by a cluster. Let's take a look at the clusters in action.

5. Clustering Colleges

When you hover over a cluster, its boundaries will appear. Clicking on a cluster will zoom to that area and the clustered colleges will spread out automatically. Eventually the map will show individual colleges in a region. These points will still be color coded and will have all of the other features that we built previously, like labels.

6. Let's practice!

Have fun testing out the searchable and clustered maps in the last few exercises of this chapter.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.