Get startedGet started for free

Setting the Default Map View

1. Setting the Default Map View

We have created several maps, but you may have noticed that they are zoomed out. Rather than having to manually zoom the map to find the area that we are most interested in, we can load it centered on a particular point with a specific zoom level. Let's take a closer look.

2. Geocoding in R

Before we set the default view of our map, we need to determine where we want our users to focus their attention. Once we select a location, like DataCamp's New York office, we can use the geocode() function from the ggmap library to geocode an address or the name of a place. We pass the geocode() function a string containing an address like, 350 5th Avenue New York, and the function returns a coordinate pair that we can use to map the location.

3. Geocoding in R II

Taking a closer look at the geocode() function and a few of its arguments, we can use the location argument, which takes a character vector containing an address or the name of a place, the output argument that allows us to select how much information will be returned, and the source argument, which let's us select if we want to use the Data Science Toolkit or Google to conduct the geocoding. If you use Google, you are agreeing to the Google Maps API Terms of Service and you should note that this API limits the number of queries you can run per day. Using this approach we can geocode place names, like Colby College, and setting the output argument to "more" will return the College's address in addition to its coordinates.

4. Setting the Default Map View

There are two common approaches to setting the default view of your map: setView() and fitBounds(). setView() allows you to pick a single point at the center of your map. Whereas fitBounds() allows us to set the view based on a rectangle. To use fitBounds(), we specify two diagonal corners of a rectangle. I typically use setView() as I find it easier to iterate through different possibilities using this approach.

5. Staying Focused

Sometimes we will want our map to remain focused on a particular geographic area. One way to accomplish this is to turn off the ability to pan the map and to limit the allowed zoom levels. Switching dragging to FALSE will prevent panning and setting the min and max zoom arguments will limit the zoom, effectively setting and maintaining the focal point, while preserving the interactive features of our web map. We will only use a small number of the available leaflet options in this course. If you are interested in learning more about different options and features, please visit RStudio's leaflet for R guide and the reference guide on the leaflet JavaScript website.

6. Restoring Focus

An alternative approach to keeping your map's focus on a particular area is to use the setMaxBounds() function instead of switching the dragging option to FALSE. If users try to pan your map outside of the max bounds they will be automatically bounced back into the boundary.

7. Let's practice!

Let's get back to making our maps!