mapview provides a few extra add* functions for use with leaflet (and mapview).

  • addMouseCoordinates - add a box with mouse position, projection information and zoom level information
  • addLogo - add images to maps
  • addHomeButton - add zoom-to button to a map
  • addFeatures - add features to a map, regardless of features type unresposnive

Here are some examples:

addMouseCoordinates

let’s us add mouse position information along with detailed information about the layer’s projection and zoom level information. By default, only lon, lat and zoom are shown. To also show info on the epsg code and the proj4string press and hold Ctrl and move the mouse. addMouseCoordinates also allows us to copy the info about the current mouse position to the clipboard by holding the Ctrl and left-clicking on the map.

library(mapview)
library(leaflet)
library(leafem)

leaflet() %>% addTiles() %>% leafem::addMouseCoordinates()

addHomeButton

can be used to add ‘zoom-to-layer’ buttons to a map. By default located in the bottom-right corner of the map (can be changed using argument position)

library(raster)

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(data = breweries91) %>% 
  addHomeButton(ext = extent(breweries91), layer.name = "Breweries")

addFeatures

is a type agnostic add* function which will call the approprate leaflet::add* function based on the provided feature type (points, lines, polygons). It is currently defined for handling objects from packages sf and sp. For sf objects, in addition to the standard feature types POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON and MULTIPOLYGON it can also handle features of type sfc_GEOMETRY which are a collection of mixed types.

library(leafem)

leaflet() %>% addTiles() %>% addFeatures(breweries)


The standard styling arguments for leaflet::add* functions can be used just like in the original add* function

library(leafem)

leaflet() %>% 
  addTiles() %>% 
  addFeatures(franconia, weight = 1, fillColor = "grey", color = "black",
              opacity = 1, fillOpacity = 0.6)