How to use leaflet advanced interactive features
In this article, the editor introduces in detail how to use the advanced interactive features of leaflet. The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the advanced interactive features of leaflet" can help you solve your doubts.
In the project of making high-quality online data map, leaflet, combined with extended HTML performance, can present very humanized dynamic effects. If it can combine decorators such as css, shiny and interactive framework, it is almost competent for the production of common dynamic interactive websites.
Library ("sp")
Library ("leaflet")
Options (stringsAsFactors = FALSE,check.names = FALSE)
Lock the directory:
Setwd ("D:/R/mapdata/State")
Import American map material (including data)
States% # sets the visual center of the rendering
AddProviderTiles ("MapBox", options = providerTileOptions (
Id = "mapbox.light", # add map layer
AccessToken = Sys.getenv ('MAPBOX_ACCESS_TOKEN')
M% >% addPolygons () # Map rendering
# Sub-box and setting color plate:
Bins% addPolygons (
FillColor = ~ pal (density)
Weight = 2
Opacity = 1
Color = "white"
DashArray = "3"
FillOpacity = 0.7
Highlight = highlightOptions (# set highlight property
Weight = 5
Color = "# 666"
DashArray = ""
FillOpacity = 0.7
BringToFront = TRUE))
# set mouse click events:
Labels% lapply (htmltools::HTML) # converted to HTML format tags
M% addPolygons (
FillColor = ~ pal (density)
Weight = 2
Opacity = 1
Color = "white"
DashArray = "3"
FillOpacity = 0.7
Highlight = highlightOptions (
Weight = 5
Color = "# 666"
DashArray = ""
FillOpacity = 0.7
BringToFront = TRUE
),
Label = labels
LabelOptions = labelOptions (# tag option setting (parameter category HTML attribute)
Style=list ("font-weight" = "normal", padding= "3px 8px")
Textsize= "15px"
Direction= "auto")
); m
# add legend:
M% >% addLegend (pal = pal, values = ~ density, opacity = 0.7, title = NULL
Position = "bottomright")
The following is the complete code for the case:
# From http://leafletjs.com/examples/choropleth/us-states.js
(data source, js format remember to save to geojson format)
Bins lapply (htmltools::HTML)
Leaflet (states)% >%
SetView (- 96,37.8,4)% >%
AddProviderTiles ("MapBox", options = providerTileOptions (
Id = "mapbox.light"
AccessToken = Sys.getenv ('MAPBOX_ACCESS_TOKEN'))% >%
AddPolygons (
FillColor = ~ pal (density)
Weight = 2
Opacity = 1
Color = "white"
DashArray = "3"
FillOpacity = 0.7
Highlight = highlightOptions (
Weight = 5
Color = "# 666"
DashArray = ""
FillOpacity = 0.7
BringToFront = TRUE)
Label = labels
LabelOptions = labelOptions (
Style = list ("font-weight" = "normal", padding = "3px 8px")
Textsize = "15px"
Direction = "auto"))% >%
AddLegend (pal = pal, values = ~ density, opacity = 0.7, title = NULL
Position = "bottomright")
After reading this, the article "how to use the advanced interactive features of leaflet" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.