本文小編為大家詳細(xì)介紹“l(fā)eaflet高級交互特性怎么用”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“l(fā)eaflet高級交互特性怎么用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)博山,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
在制作高質(zhì)量在線數(shù)據(jù)地圖的項(xiàng)目中,leaflet結(jié)合擴(kuò)展的HTML性能,可以呈現(xiàn)非常人性化的動態(tài)效果,如能結(jié)合css、shiny等裝飾器和交互框架,幾乎可以勝任常見的動態(tài)交互網(wǎng)站的制作。
library("sp")
library("leaflet")
options(stringsAsFactors = FALSE,check.names = FALSE)
鎖定目錄:
setwd("D:/R/mapdata/State")
導(dǎo)入美國地圖素材(含數(shù)據(jù))
states<-geojsonio::geojson_read("us-states.geojson", what = "sp")
一個簡單的開始:(引用mapbox地圖)
m <- leaflet(states) %>%
setView(-96, 37.8, 4) %>% #設(shè)置呈現(xiàn)的視覺中心
addProviderTiles("MapBox", options = providerTileOptions(
id = "mapbox.light", #添加地圖圖層
accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')))
m %>% addPolygons() #地圖呈現(xiàn)
#分箱及設(shè)置色盤:
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = states$density, bins = bins)
m %>% addPolygons(
fillColor = ~pal(density), #顯式聲明顏色映射變量
weight = 2, #線寬
opacity = 1, #透明度
color = "white", #顏色
dashArray = "3",
fillOpacity = 0.7) #填充透明度
m %>% addPolygons(
fillColor = ~pal(density),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlight = highlightOptions( #設(shè)置高亮屬性
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE))
#設(shè)置鼠標(biāo)點(diǎn)擊事件:
labels <- sprintf("%s
%g people / mi2",states$name,states$density) %>% lapply(htmltools::HTML) #轉(zhuǎn)化為HTML格式標(biāo)簽
m <-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( #標(biāo)簽選項(xiàng)設(shè)置(參數(shù)類別HTML屬性)
style=list("font-weight"="normal",padding="3px 8px"),
textsize="15px",
direction="auto")
);m
#增加圖例:
m %>% addLegend(pal = pal, values = ~density, opacity = 0.7, title = NULL,
position = "bottomright")
以下是該案例的完整代碼:
# From http://leafletjs.com/examples/choropleth/us-states.js
(數(shù)據(jù)源,js格式記得保存為geojson格式)
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = states$density, bins = bins)
labels <- sprintf(
"%s
%g people / mi2",states$name, states$density
) %>% 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")
讀到這里,這篇“l(fā)eaflet高級交互特性怎么用”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。