Setting the Default Map View

Interactive Maps with leaflet in R

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

Geocoding in R

  • A common approach is to use the geocode() function in the ggmap package
  • Returns the latitude and longitude of an address or a place name
library(ggmap)

geocode("350 5th Ave, New York, NY 10118")
Information from URL : https://maps.googleapis.com/maps/api/geocode/...

lon       lat
-73.98575 40.74856
Interactive Maps with leaflet in R

Geocoding in R II

geocode(location, 
        output = c("latlon", "latlona", "more", "all"),
        source = c("google", "dsk"))

geocode("Colby College", output = "more", source = "google")
lon       lat      type          loctype  
-69.66264 44.56387 establishment rooftop  

address
4000 mayflower hill dr, waterville, me 04901, usa
Interactive Maps with leaflet in R

Setting the Default Map View

setView()

leaflet() %>% 
 addTiles() %>% 
 setView(lng = -73.98575, 
         lat = 40.74856, 
         zoom = 13)

setView() example

fitBounds()

leaflet() %>% 
 addTiles() %>% 
 fitBounds(
  lng1 = -73.910, lat1 = 40.773, 
  lng2 = -74.060, lat2 = 40.723)

fitBounds() example

Interactive Maps with leaflet in R

Staying Focused

leaflet(options = leafletOptions(dragging = FALSE,
                                 minZoom = 14, 
                                 maxZoom = 18))  %>% 
    addProviderTiles("CartoDB")  %>% 
    setView(lng = -73.98575, lat = 40.74856, zoom = 18)

Interactive Maps with leaflet in R

Restoring Focus

leaflet()  %>% 
     addTiles()  %>% 
     setView(lng = -73.98575, lat = 40.74856, zoom = 18) %>% 
     setMaxBounds(lng1 = -73.98575, lat1 = 40.74856, 
                  lng2 = -73.98575, lat2 = 40.74856) 

setMaxBounds() example

Interactive Maps with leaflet in R

Let's practice!

Interactive Maps with leaflet in R

Preparing Video For Download...