我用 R 中的模拟数据制作了以下地图:
首先我加载了库:
library(leaflet)
library(leaflet.extras)
library(dplyr)
Run Code Online (Sandbox Code Playgroud)
然后,我模拟了这个例子的随机数据:
myFun <- function(n = 5000) {
a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}
Lat = abs(rnorm(1000, 42.2, 0.1))
Long = abs(rnorm(1000, -70, 0.1))
City = myFun(1000)
cities = data.frame(Lat, Long, City)
Run Code Online (Sandbox Code Playgroud)
最后我做了一张地图:
# download icon from here: https://leafletjs.com/examples/custom-icons/leaf-green.png
leaflet(cities) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addMarkers( clusterOptions = markerClusterOptions(), popup = ~paste("title: ", City)) %>%
addResetMapButton() %>%
# these markers will be "invisible" on the map:
addMarkers(
data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
group = 'cities', # this is the group to use in addSearchFeatures()
# make custom icon that is so small you can't see it:
icon = makeIcon(
iconUrl = "leaf-green.png",
iconWidth = 1, iconHeight = 1
)
) %>%
addSearchFeatures(
targetGroups = 'cities', # group should match addMarkers() group
options = searchFeaturesOptions(
zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
)
)
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用此地图中的搜索功能并查找单个点(例如“QNZLF4655A”):
所有其他点都同时显示 - 有没有办法防止这种情况并仅显示正在搜索的点(即蓝色图钉)?
谢谢你!
注意:有时,如果地图“缩小”太多 - 当我在搜索栏中搜索城市时,会短暂出现红色圆圈,并且搜索到的城市根本不会出现:
我希望当搜索一个城市时,地图会自动显示该城市及其“识别标签”。有没有办法来解决这个问题?
据我所知,您的搜索缩放级别和调用集群图标中的弹出窗口指定有点相互矛盾。
首先,您每次搜索时没有看到弹出窗口和/或标记的原因:
您可以增加搜索缩放级别。(由于标记的接近......)
- 或者 -
您可以将弹出内容声明移至第二次addMarkers调用。
其次,隐藏标记...
看起来除了弹出窗口之外,我还有一个输入框,其中包含可用于控制标记的外观(或缺乏外观)的信息。然而,搜索框输入似乎位于封闭的shadowdom后面。(这意味着我无法使用那里的信息。)
我做了什么:
我将弹出窗口声明移至第二次addMarkers调用,并保持缩放级别不变。当您搜索时,您会在图标所在的位置看到弹出窗口和阴影。关闭弹出窗口后,您将再次看到所有图标。如果搜索到的图标仍然分组,它将保持分组(基于缩放和标记接近度)。
但是,在关闭弹出窗口后,无论您是否使用默认的autoCollapse. (我已在此代码中注释掉了此调用。)
leaflet(cities) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addMarkers(clusterOptions = markerClusterOptions()) %>%
# REMOVED , popup = ~paste("title: ", City)
addResetMapButton() %>%
# these markers will be "invisible" on the map:
addMarkers(
data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
group = 'cities', # this is the group to use in addSearchFeatures()
# make custom icon that is so small you can't see it:
icon = makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 1, iconHeight = 1
), popup = ~paste0("title: ", City) # <--- I'm new!
) %>%
addSearchFeatures(
targetGroups = 'cities', # group should match addMarkers() group
options = searchFeaturesOptions(
zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
hideMarkerOnCollapse = TRUE #, autoCollapse = TRUE # removed
)
) %>%
htmlwidgets::onRender(
"function(el, x){
$('div.leaflet-pane.leaflet-popup-pane').bind(\"DOMSubtreeModified\", function() {
marks = document.querySelector('div.leaflet-pane.leaflet-marker-pane');
if(this.innerHTML) { /* add the event to the DOM */
marks.style.visibility = 'hidden'; /* if popup */
} else {
marks.style.visibility = 'visible'; /* no popup */
}
})}")
Run Code Online (Sandbox Code Playgroud)
哈萨克斯坦南部伦格尔附近的这些舒适的地方。