admin管理员组文章数量:1394099
Within the leaflet package for R, is there a way to click on a marker, and be directed to a URL?*
Here's the JS solution.
In R, to add a Popup with a URL:
library(leaflet)
content <- paste(sep = "<br/>",
"<b><a href=''>Samurai Noodle</a></b>"
)
leaflet() %>% addTiles() %>%
addPopups(-122.327298, 47.597131, content,
options = popupOptions(closeButton = FALSE)
)
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 ...
?
Lastly, how could a custom JS function display different URLs for each map marker? Consider the example data.frame:
df <- data.frame(url = c("",
"")),
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.
Within the leaflet package for R, is there a way to click on a marker, and be directed to a URL?*
Here's the JS solution.
In R, to add a Popup with a URL:
library(leaflet)
content <- paste(sep = "<br/>",
"<b><a href='http://www.samurainoodle.'>Samurai Noodle</a></b>"
)
leaflet() %>% addTiles() %>%
addPopups(-122.327298, 47.597131, content,
options = popupOptions(closeButton = FALSE)
)
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 ...
?
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./questions/tagged/python",
"https://stackoverflow./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.
Share Improve this question edited Apr 16, 2020 at 6:48 Rich Pauloo asked Oct 3, 2019 at 0:04 Rich PaulooRich Pauloo 8,4524 gold badges41 silver badges76 bronze badges 1- 1 I think you're going to have inject an event handler in javascript, using rstudio.github.io/leaflet/… . I would webshot or build a simple leaflet example to get the javascript worked out outside of R. Then bring that code in. – wildintellect Commented Oct 4, 2019 at 23:35
1 Answer
Reset to default 9 +50You could use htmltools
or htmlwidgets
to add an onclick
event with javascript:
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.")
};
marker[0].addEventListener("click", openLink, false);
})
'
)
),
map
)
)
)
Solution 2 - with 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.")
};
marker[0].addEventListener("click", openLink, false);
}
')
Different url for each marker:
This is a dirty approach and shows the general way. I lack time to get fortable with closures in JS again to add a loop.
One could look here: addEventListener using for loop and passing values. And data can be passed from R to JS with the onRender function.
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./questions/tagged/python");};
marker[1].onclick=function(){window.open("https://stackoverflow./questions/tagged/r");};
}
')
library(leaflet)
library(htmlwidgets)
leaflet() %>%
addTiles() %>%
addMarkers(lng = df$lng, lat = df$lat, popup = 'LINK',
options = popupOptions(closeButton = FALSE)
) %>%
onRender(jsCode)
Using the approach from addEventListener using for loop and passing values, you can loop through the data to get different a url for each marker:
library(leaflet)
library(htmlwidgets)
df <- data.frame(url = c("https://stackoverflow./questions/tagged/python",
"https://stackoverflow./questions/tagged/r"),
lng = c(-122.327298, -122.337298),
lat = c(47.597131,47.587131))
jsCode <- '
function(el, x, data) {
var marker = document.getElementsByClassName("leaflet-marker-icon leaflet-zoom-animated leaflet-interactive");
for(var i=0; i < marker.length; i++){
(function(){
var v = data.url[i];
marker[i].addEventListener("click", function() { window.open(v);}, false);
}()
);
}
}'
leaflet() %>%
addTiles() %>%
addMarkers(lng = df$lng, lat = df$lat, popup = 'LINK',
options = popupOptions(closeButton = FALSE)
) %>%
onRender(jsCode, data=df)
本文标签: javascriptClicking a leaflet marker takes you to URLStack Overflow
版权声明:本文标题:javascript - Clicking a leaflet marker takes you to URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744738175a2622455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论