单击R Leaflet中的“插件"脉冲标记时添加一个弹出窗口 [英] Add a popup when clicked through to a 'plugin' pulsing marker in R Leaflet
本文介绍了单击R Leaflet中的“插件"脉冲标记时添加一个弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我(在SO的帮助下)设法从带有R传单包的地图插件,其脚本如下:
I managed (with the help of SO) to put a pulsing marker from a plugin to a map with the R leaflet package with the following script :
library(leaflet)
library(htmltools)
library(htmlwidgets)
# This tells htmlwidgets about our plugin name, version, and
# where to find the script. (There's also a stylesheet argument
# if the plugin comes with CSS files.)
esriPlugin <- htmlDependency("leaflet-icon-pulse",version = "1.0",
src = "realtimemap/",
script = "L.Icon.Pulse.js",stylesheet ="L.Icon.Pulse.css")
# A function that takes a plugin htmlDependency object and adds
# it to the map. This ensures that however or whenever the map
# gets rendered, the plugin will be loaded into the browser.
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
content <- paste(sep = "<br/>",
"<b><a> Homicide </a></b>",
"606 5th Ave. S",
"Ocean District, CA 98138"
)
leaflet() %>% addProviderTiles("CartoDB.DarkMatter") %>% setView(-122.4105513,37.78250256, zoom = 12) %>%
addPopups(-122.41, 37.78, content,
options = popupOptions(closeButton = TRUE)
) %>%
registerPlugin(esriPlugin) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el,x) {
var pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red'});
var pulsingIcon2 = L.icon.pulse({iconSize:[15,15],color:'orange',heartbeat:2});
var marker = L.marker([37.78,-122.41],{icon: pulsingIcon}).addTo(this);
var marker = L.marker([37.75,-122.39],{icon: pulsingIcon2}).addTo(this);}")
但是,现在,如果我单击那些脉动标记,我想显示一些信息.当然,我不能使用传单包中的addpopup或addmarker,因为我必须使用非传统的方法从插件中添加标记.你会怎么做?
But now, I would like to display some information if I click on those pulsing markers. Of course, I can't use addpopup nor addmarker from the leaflet package since I had to use an unorthodox way to add a marker from a plugin. How would you do that?
推荐答案
好,我发现了一些东西. 通过这样做,它可以工作!!!
Ok, I found something. By doing this, it works!!!
onRender("function(el,x) {
var pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red',heartbeat:0.5});
var pulsingIcon2 = L.icon.pulse({iconSize:[10,10],color:'orange',heartbeat:2});
var marker = L.marker([37.78,-122.41],{icon: pulsingIcon}).bindPopup('<b>Hello world!</b><br>I am a popup.').openPopup().addTo(this);
var marker = L.marker([37.75,-122.39],{icon: pulsingIcon2}).addTo(this);
}")
这篇关于单击R Leaflet中的“插件"脉冲标记时添加一个弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文