单击传单标记会将您带到 URL

Clicking a leaflet marker takes you to URL(单击传单标记会将您带到 URL)

本文介绍了单击传单标记会将您带到 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 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.

推荐答案

你可以使用 htmltoolshtmlwidgets 添加一个 onclick 事件javascript:

You could use htmltools or htmlwidgets to add an onclick event with javascript:

解决方案 1) 使用 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.

 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)
 

使用 addEventListener 中的方法,使用 for 循环和传递值,您可以遍历数据以获取每个标记的不同 url:

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.com/questions/tagged/python",
                         "https://stackoverflow.com/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)

这篇关于单击传单标记会将您带到 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:单击传单标记会将您带到 URL

基础教程推荐