问题描述
这是 JS解决方案.
在R中,添加带有URL的弹出窗口:
In R, to add a Popup with a URL:
library(leaflet)
content <- paste(sep = "<br/>",
"<b><a href='http://www.samurainoodle.com'>Samurai Noodle</a></b>"
)
leaflet() %>% addTiles() %>%
addPopups(-122.327298, 47.597131, content,
options = popupOptions(closeButton = FALSE)
)
添加一个标记,也很简单,当单击该标记时,将在弹出窗口中提供一个URL:
It's also straightforward to add a Marker that, when clicked, provides a URL in the popup:
leaflet() %>% addTiles() %>%
addMarkers(-122.327298, 47.597131, popup = content,
options = popupOptions(closeButton = FALSE)
)
也许自定义项已传递给...
中的传单?
Perhaps something custom passed to leaflet in ...
?
最后,自定义JS函数如何为每个地图标记显示不同的URL?考虑示例data.frame:
Lastly, how could a custom JS function display different URLs for each map marker? Consider the example data.frame:
df <- data.frame(url = c("https://stackoverflow.com/questions/tagged/python",
"https://stackoverflow.com/questions/tagged/r")),
lng = c(-122.327298, -122.337298),
lat = c(47.597131,47.587131))
*这是先前询问的,但我是在问这个问题再次在这里做一个最小的,可复制的例子.
*This was previously asked, but I'm asking the question again here and making a minimal, reproducible example.
推荐答案
您可以使用htmltools
或htmlwidgets
通过javascript添加onclick
事件:
You could use htmltools
or htmlwidgets
to add an onclick
event with javascript:
解决方案1)和htmltools
:
Solution 1) with htmltools
:
library(leaflet)
map <- leaflet() %>%
addTiles() %>%
addMarkers(-122.327298, 47.597131, popup = 'LINK',
options = popupOptions(closeButton = FALSE)
)
library(htmltools)
browsable(
tagList(
list(
tags$head(
tags$script(
'
document.addEventListener("DOMContentLoaded", function(){
var marker = document.getElementsByClassName("leaflet-pane leaflet-marker-pane");
var openLink = function() {
window.open("https://www.stackoverflow.com")
};
marker[0].addEventListener("click", openLink, false);
})
'
)
),
map
)
)
)
解决方案2-带有htmlwidgets:
library(leaflet)
library(htmlwidgets)
leaflet() %>%
addTiles() %>%
addMarkers(-122.327298, 47.597131, popup = 'LINK',
options = popupOptions(closeButton = FALSE)
) %>%
onRender('
function(el, x) {
var marker = document.getElementsByClassName("leaflet-pane leaflet-marker-pane");
var openLink = function() {
window.open("https://www.stackoverflow.com")
};
marker[0].addEventListener("click", openLink, false);
}
')
每个标记的网址不同:
这是一种肮脏的方法,它显示了一般的方法.我没有时间去适应JS中的闭包以添加循环.
This is a dirty approach and shows the general way. I lack time to get comfortable with closures in JS again to add a loop.
可以在这里查看: addEventListener使用for循环和传递值.并且可以使用onRender函数将数据从R传递到JS.也许我会在周末再次找时间,或者您/某人添加了这一部分.随时公开问题,..
One could look here: addEventListener using for loop and passing values. And data can be passed from R to JS with the onRender function. Maybe i find time on the weekend again or you/someone adds this part. Feel free to leave the question open,..
jsCode <- paste0('
function(el, x) {
var marker = document.getElementsByClassName("leaflet-marker-icon leaflet-zoom-animated leaflet-interactive");
marker[0].onclick=function(){window.open("https://stackoverflow.com/questions/tagged/python");};
marker[1].onclick=function(){window.open("https://stackoverflow.com/questions/tagged/r");};
}
')
library(leaflet)
library(htmlwidgets)
leaflet() %>%
addTiles() %>%
addMarkers(lng = df$lng, lat = df$lat, popup = 'LINK',
options = popupOptions(closeButton = FALSE)
) %>%
onRender(jsCode)
这篇关于单击传单标记将您带到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!