問題描述
我做錯了什么,或者為什么下面的例子不起作用?我正在嘗試使用選項 iconCreateFunction
使 Leaflet markercluster 插件在 R Shiny 應用程序中與 leafletProxy()
一起使用.插件是否無法使用 leafletProxy()
向地圖添加自定義圖標標記?
當我按下第一個按鈕并在下面的示例中縮小時,我收到一條錯誤消息:
<塊引用>TypeError: this._group.options.iconCreateFunction 不是函數
我嘗試從 markercluster 復制
修改方案
iconCreateFunction
的行為在 leafletProxy
中使用時絕對是 flakey.雖然我認為某些瀏覽器中存在緩存,因此難以直觀地跟蹤.
為了消除您遇到的 javascript 錯誤,應用 layerId
和 clusterId
值以及使用 removeMarker
代替 clearMarkers
.
注意我的解決方案的一個奇怪的副作用是重新繪制時會掉下一個標記,我有點累了,稍后再看看.這個問題可能是微不足道的,也可能不是微不足道的.
app.R
庫(閃亮)圖書館(dplyr)圖書館(傳單)ui <-流體頁面(titlePanel("你好閃亮!"),側邊欄布局(側邊欄面板(actionButton(inputId = "my_button1",label = "使用leafletProxy()"),actionButton(inputId = "my_button2",label = "使用 renderLeaflet()")),主面板(傳單輸出(outputId = "我的地圖",寬度 = "100%",高度 = "300 像素"))))服務器 <- 功能(輸入,輸出,會話){some_data <- data.frame(lon = c(4.905167, 4.906357, 4.905831),緯度 = c(52.37712, 52.37783, 52.37755),number_var = c(5, 9, 7),名稱 = c(簡"、哈羅德"、邁克")、字符串AsFactors = FALSE)marker_js <- JS("函數(簇){var html = '<div 樣式="背景色:rgba(77,77,77,0.5)"><span>'+ cluster.getChildCount() + '</div><span>'return new L.DivIcon({html: html, className: 'marker-cluster'});}")output$mymap <- renderLeaflet({傳單(一些數據)%>%addProviderTiles(providers$CartoDB.Positron) %>%適合邊界(?分鐘(經度),?分鐘(緯度),?最大(經度),?最大(緯度))%>%添加標記(layerId = "我的圖層",clusterId = "我的集群",lng = ~lon,緯度=?緯度,clusterOptions = markerClusterOptions(iconCreateFunction = marker_js))})觀察事件(輸入$my_button1,{leafletProxy("mymap", data = some_data) %>%removeMarker(layerId = "mylayer") %>%清除瓷磚 %>%addProviderTiles(providers$CartoDB.Positron) %>%適合邊界(?分鐘(經度),?分鐘(緯度),?最大(經度),?最大(緯度))%>%添加標記(layerId = "我的圖層",clusterId = "我的集群",lng = ~lon,緯度=?緯度,clusterOptions = markerClusterOptions(iconCreateFunction = marker_js))})觀察事件(輸入$my_button2,{output$mymap <- renderLeaflet({傳單(一些數據)%>%addProviderTiles(providers$CartoDB.Positron) %>%適合邊界(?分鐘(經度),?分鐘(緯度),?最大(經度),?最大(緯度))%>%添加標記(layerId = "我的圖層",clusterId = "我的集群",lng = ~lon,緯度=?緯度,clusterOptions = markerClusterOptions(iconCreateFunction = marker_js))})})}閃亮應用(用戶界面 = 用戶界面,服務器 = 服務器)
瀏覽器內
<塊引用>沒有發現其他 javascript 錯誤.
Am I doing something wrong, or why does the below example not work? I am trying to make leaflet markercluster plugin work with leafletProxy()
in an R Shiny app, using the option iconCreateFunction
. Is the plugin not capable of adding customized icon markers to the map using leafletProxy()
?
When I press the first button and zoom out in below example, I get an error saying:
TypeError: this._group.options.iconCreateFunction is not a function
I tried to copy the original example from the markercluster documentation:
library(shiny)
library(dplyr)
library(leaflet)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
actionButton(inputId = "my_button1",
label = "Use leafletProxy()"),
actionButton(inputId = "my_button2",
label = "Use renderLeaflet()")
),
mainPanel(
leafletOutput(
outputId = "map",
width = "100%",
height = "300px"
)
)
)
)
server <- function(input, output, session) {
some_data <- data.frame(
"lon"=c(4.905167,4.906357,4.905831),
"lat"=c(52.37712,52.37783,52.37755),
"number_var"=c(5,9,7),
"name"=c("Jane","Harold","Mike"),
stringsAsFactors = F
)
output$map <- renderLeaflet({
return(
leaflet(data = some_data[0,]) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
fitBounds(
min(some_data$lon),
min(some_data$lat),
max(some_data$lon),
max(some_data$lat)
) %>%
addMarkers(
lng = ~lon,
lat = ~lat,
clusterOptions = markerClusterOptions(
iconCreateFunction = JS(paste0("function(cluster) {",
"return new L.DivIcon({",
"html: '<div style="background-color:rgba(77,77,77,0.5)"><span>' + cluster.getChildCount() + '</div><span>',",
"className: 'marker-cluster'",
"});",
"}"))
)
)
)
})
observeEvent(input$my_button1,{
leafletProxy(mapId = "map",
session = session,
data = some_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
clearMarkerClusters() %>%
clearMarkers() %>%
fitBounds(
min(some_data$lon),
min(some_data$lat),
max(some_data$lon),
max(some_data$lat)
) %>%
addMarkers(
lng = ~lon,
lat = ~lat,
clusterOptions = markerClusterOptions(
iconCreateFunction = JS(paste0("function(cluster) {",
"console.log('Here comes cluster',cluster); ",
"return new L.DivIcon({",
"html: '<div style="background-color:rgba(77,77,77,0.5)"><span>' + cluster.getChildCount() + '</div><span>',",
"className: 'marker-cluster'",
"});",
"}"))
)
)
})
observeEvent(input$my_button2,{
output$map <- renderLeaflet({
leaflet(data = some_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
fitBounds(
min(some_data$lon),
min(some_data$lat),
max(some_data$lon),
max(some_data$lat)
) %>%
addMarkers(
lng = ~lon,
lat = ~lat,
clusterOptions = markerClusterOptions(
iconCreateFunction = JS(paste0("function(cluster) {",
"console.log('Here comes cluster',cluster); ",
"return new L.DivIcon({",
"html: '<div style="background-color:rgba(77,77,77,0.5)"><span>' + cluster.getChildCount() + '</div><span>',",
"className: 'marker-cluster'",
"});",
"}"))
)
)
})
})
}
shinyApp(ui = ui, server = server)
Package versions:
dplyr_0.7.4
leaflet_1.1.0
shiny_1.0.5
R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.3 LTS
Browser version: Firefox Quantum 57.0.1 (64-bit)
Revised solution
The behaviour of iconCreateFunction
is definitely flakey when used within leafletProxy
. Although I think there is caching in some browsers making it difficult to track visually.
In order to eliminate the javascript error you were experiencing, it is important to apply layerId
and clusterId
values as well as using removeMarker
in lieu of clearMarkers
.
N.B. A strange side-effect of my solution is that a marker is dropped when re-drawn, I'm getting a bit tired and will have another look later. That problem may or may not be trivial.
app.R
library(shiny)
library(dplyr)
library(leaflet)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
actionButton(inputId = "my_button1",
label = "Use leafletProxy()"),
actionButton(inputId = "my_button2",
label = "Use renderLeaflet()")
),
mainPanel(
leafletOutput(
outputId = "mymap",
width = "100%",
height = "300px"
))
))
server <- function(input, output, session) {
some_data <- data.frame(
lon = c(4.905167, 4.906357, 4.905831),
lat = c(52.37712, 52.37783, 52.37755),
number_var = c(5, 9, 7),
name = c("Jane", "Harold", "Mike"),
stringsAsFactors = FALSE
)
marker_js <- JS("function(cluster) {
var html = '<div style="background-color:rgba(77,77,77,0.5)"><span>' + cluster.getChildCount() + '</div><span>'
return new L.DivIcon({html: html, className: 'marker-cluster'});
}")
output$mymap <- renderLeaflet({
leaflet(some_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
fitBounds(
~min(lon),
~min(lat),
~max(lon),
~max(lat)
) %>%
addMarkers(
layerId = "mylayer",
clusterId = "mycluster",
lng = ~lon,
lat = ~lat,
clusterOptions = markerClusterOptions(
iconCreateFunction = marker_js
)
)
})
observeEvent(input$my_button1, {
leafletProxy("mymap", data = some_data) %>%
removeMarker(layerId = "mylayer") %>%
clearTiles %>%
addProviderTiles(providers$CartoDB.Positron) %>%
fitBounds(
~min(lon),
~min(lat),
~max(lon),
~max(lat)
) %>%
addMarkers(
layerId = "mylayer",
clusterId = "mycluster",
lng = ~lon,
lat = ~lat,
clusterOptions = markerClusterOptions(
iconCreateFunction = marker_js
)
)
})
observeEvent(input$my_button2,{
output$mymap <- renderLeaflet({
leaflet(some_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
fitBounds(
~min(lon),
~min(lat),
~max(lon),
~max(lat)
) %>%
addMarkers(
layerId = "mylayer",
clusterId = "mycluster",
lng = ~lon,
lat = ~lat,
clusterOptions = markerClusterOptions(
iconCreateFunction = marker_js
)
)
})
})
}
shinyApp(ui = ui, server = server)
in-browser
No other javascript errors were noted.
這篇關于markercluster 是否與 LeafletProxy() 和選項 iconCreateFunction 一起使用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!