Get startedGet started for free

The Leaflet Extras Package

1. The leaflet.extras Package

In Chapter 3, we will be expanding our map of colleges to include every four-year college in America. But first we are going to have a little fun, adding a few pieces of flair to our map, so that we can answer the age-old question, where is the world's largest ball of twine?

2. The leaflet.extras Package

The leaflet extras package provides a set of useful functions that extend the leaflet package. There are many useful functions in this package, but to get started we will focus on just three. First, we will make our map searchable, then will enable geocoding via mouse clicks, and finally, we'll add a button to allow us to easily restore the default map view.

3. addSearchOSM()

To use OpenStreetMap to find the world's largest of ball of twine, we can pipe our leaflet map into the addSearchOSM() function. This will add a magnifying glass icon to our map. Clicking this icon will reveal a search box that we can use to search all of the locations included on our map. It looks like there are two claims on the world's largest ball of twine.

4. Find the Twine

I got the scoop from a Kansan that they have the real record holder. Clicking on that entry in the search menu automatically zooms us in to the location. Then, just to confirm, I took a look at Wikipedia and it turns out that it's a big ball of twine.

5. addReverseSearchOSM()

In Chapter 1, we used the geocode() function from the ggmaps package to find a location’s coordinates. With one additional line of code, the addReverseSearchOSM() function makes it possible to use OpenStreetMap to geocode simply by clicking on our leaflet map. We can combine the OSM search and geocoding functions to find the coordinates of another American highway icon, Carhenge. Clicking on the location will drop a pin and the latitude and longitude will appear in the pin's label.

6. addResetMapButton()

When you are searching and zooming in on locations, it's often helpful to have a shortcut to get back to the default map view. Using the addResetMapButton() function will create a reset icon below our magnifying glass that we can use to quickly return to the default view.

7. Let's practice!

Now it's your turn to have some fun with the leaflet extras package.