結(jié)果是交互式html文檔

互動(dòng)地圖截圖
原始數(shù)據(jù)

原始數(shù)據(jù)
R語言代碼
# Sat Jun 7 14:39:58 2025 edit
# 字符編碼:UTF-8
# R 版本:R 4.4.2 x64 for window 11
# cgh163email@163.com
# 個(gè)人筆記不負(fù)責(zé)任,拎了個(gè)梨????
#.rs.restartR()
require(openxlsx)
rm(list = ls());gc()
library(leaflet)
require(leafletCN)
library(htmltools)
dt <- read.xlsx("原始數(shù)據(jù)/工作簿2.xlsx")
gzmap <- sf::st_read('原始數(shù)據(jù)/gz.min.geojson')
plot(gzmap$geometry)
# 示例數(shù)據(jù)(含兩個(gè)鏈接)
attractions <- data.frame(
name = c("故宮", "外灘", "西湖", "長城"),
lng = c(116.3974, 121.4902, 120.1551, 116.5704),
lat = c(39.9183, 31.2397, 30.2741, 40.4319),
url1 = c(
"https://www.dpm.org.cn/",
"https://www.trip.com/travel-guide/shanghai/the-bund-1401261/",
"https://www.hangzhou.gov.cn/col/col1228980242/index.html",
"https://www.thegreatwall.com.cn/"
),
link_text1 = c("官網(wǎng)", "旅游攻略", "政務(wù)網(wǎng)", "官方網(wǎng)站"),
url2 = c(
"https://baike.baidu.com/item/%E5%8C%97%E4%BA%AC%E6%95%85%E5%AE%AB/127716",
"https://baike.baidu.com/item/%E5%A4%96%E6%BB%A9/1696305",
"https://baike.baidu.com/item/%E8%A5%BF%E6%B9%96/142748",
"https://baike.baidu.com/item/%E4%B8%87%E9%87%8C%E9%95%BF%E5%9F%8E/1420"
),
link_text2 = c("百度百科", "百度百科", "百度百科", "百度百科")
)
head(attractions)
# 創(chuàng)建地圖
leaflet(attractions) |>
addTiles() |>
addMarkers(
lng = ~lng,
lat = ~lat,
label = ~name,
popup = ~paste0(
"<div style='min-width:150px'>",
"<b>", name, "</b><br><br>",
"<span style='display: inline-block; margin-bottom: 5px;'>",
"?? <a href='", url1, "' target='_blank' style='color: #0066cc;'>", link_text1, "</a>",
"</span><br>",
"<span style='display: inline-block;'>",
"?? <a href='", url2, "' target='_blank' style='color: #cc3300;'>", link_text2, "</a>",
"</span>",
"</div>"
),
clusterOptions = markerClusterOptions()
) |>
addControl(
html = "<div style='padding:5px;background:white;border:1px solid gray;'>
<b>點(diǎn)擊標(biāo)記查看雙鏈接</b><br>
<small>藍(lán)色鏈接:官方資源 | 紅色鏈接:百科資料</small>
</div>",
position = "topright"
)
# Sat Jun 7 14:45:26 2025 ------------------------------
head(dt)
# name lng lat urlinf txt.inf urlamap txt.gps
# (溫室也免)中國科學(xué)院華南植物園 113.364581 23.182387 https://mp.weixin.qq.com/s?__biz=MzUzMjkwMTU5Mw==&mid=2247486919&idx=2&sn=e5bd8a518107fb8cef25c4218a4d6c0f&scene=21#wechat_redirect 推文介紹 https://uri.amap.com/marker?position=113.364581,23.182387&name=(溫室也免)中國科學(xué)院華南植物園&src=梨子定位&coordinate=gaode&callnative=1 高德地圖
# 白江湖森林公園 113.896809 23.507591 https://mp.weixin.qq.com/s/H1509RbFM9zVk5-P6qGLSA 推文介紹 https://uri.amap.com/marker?position=113.896809,23.507591&name=白江湖森林公園&src=梨子定位&coordinate=gaode&callnative=1 高德地圖
# Sat Jun 7 15:11:40 2025 +++++++++++++++++++
leaflet(dt) |>
amap() |>
addMarkers(
lng = ~lng,
lat = ~lat,
label = ~name,
popup = ~paste0(
"<div style='min-width:150px'>",
"<b>", name, "</b><br><br>",
"<span style='display: inline-block; margin-bottom: 5px;'>",
"?? <a href='", urlinf, "' target='_blank' style='color: #0066cc;'>", txt.inf, "</a>",
"</span><br>",
"<span style='display: inline-block;'>",
"?? <a href='", urlamap, "' target='_blank' style='color: #cc3300;'>", txt.gps, "</a>",
"</span>",
"</div>"
),
clusterOptions = markerClusterOptions()
)
# Sat Jun 7 15:52:33 2025 +++++++++++++++++++
# options = popupOptions(maxWidth = 300, minWidth = 250)
lmap <- leaflet(dt) |>
amap() |>
addMarkers(
# options = popupOptions(maxWidth = 300, minWidth = 250),
popup = ~paste0(
"<div style='min-width:200px; font-size:16px;'>",
"<b>", name, "</b><br><br>",
"<button onclick=\"window.open('", urlinf, "','_blank')\"
style='background-color: #0066cc; color: white; border: none;
padding: 10px; margin: 5px 0; width: 100%;
border-radius: 5px; font-size:16px;'>
?? ", txt.inf, "
</button><br>",
"<button onclick=\"window.open('", urlamap, "','_blank')\"
style='background-color: #cc3300; color: white; border: none;
padding: 10px; margin: 5px 0; width: 100%;
border-radius: 5px; font-size:16px;'>
?? ", txt.gps, "
</button>",
"</div>"
),
clusterOptions = markerClusterOptions() # 點(diǎn)聚合功能
) |>
addPolygons(
data = gzmap, # 你的sf對(duì)象
color = "#baddf1", # 邊界線顏色(紅色)
weight = 3, # 邊界線寬度
opacity = 1, # 邊界線不透明度
fillColor = "transparent", # 填充顏色(透明)
fillOpacity = 0, # 填充不透明度
label = ~鄉(xiāng)鎮(zhèn)名 # 懸停標(biāo)簽(使用sf對(duì)象中的name字段)
)
lmap
# 保存為HTML
library(htmlwidgets)
saveWidget(lmap, file = "廣州獻(xiàn)血優(yōu)待證景點(diǎn)地圖.html")