1. <legend id='GUcZw'><style id='GUcZw'><dir id='GUcZw'><q id='GUcZw'></q></dir></style></legend>

      <i id='GUcZw'><tr id='GUcZw'><dt id='GUcZw'><q id='GUcZw'><span id='GUcZw'><b id='GUcZw'><form id='GUcZw'><ins id='GUcZw'></ins><ul id='GUcZw'></ul><sub id='GUcZw'></sub></form><legend id='GUcZw'></legend><bdo id='GUcZw'><pre id='GUcZw'><center id='GUcZw'></center></pre></bdo></b><th id='GUcZw'></th></span></q></dt></tr></i><div id='GUcZw'><tfoot id='GUcZw'></tfoot><dl id='GUcZw'><fieldset id='GUcZw'></fieldset></dl></div>

      <small id='GUcZw'></small><noframes id='GUcZw'>

      <tfoot id='GUcZw'></tfoot>
      • <bdo id='GUcZw'></bdo><ul id='GUcZw'></ul>

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

      时间:2023-08-09
      • <legend id='7tNKW'><style id='7tNKW'><dir id='7tNKW'><q id='7tNKW'></q></dir></style></legend>

          <i id='7tNKW'><tr id='7tNKW'><dt id='7tNKW'><q id='7tNKW'><span id='7tNKW'><b id='7tNKW'><form id='7tNKW'><ins id='7tNKW'></ins><ul id='7tNKW'></ul><sub id='7tNKW'></sub></form><legend id='7tNKW'></legend><bdo id='7tNKW'><pre id='7tNKW'><center id='7tNKW'></center></pre></bdo></b><th id='7tNKW'></th></span></q></dt></tr></i><div id='7tNKW'><tfoot id='7tNKW'></tfoot><dl id='7tNKW'><fieldset id='7tNKW'></fieldset></dl></div>

            <small id='7tNKW'></small><noframes id='7tNKW'>

          1. <tfoot id='7tNKW'></tfoot>

              <tbody id='7tNKW'></tbody>

              <bdo id='7tNKW'></bdo><ul id='7tNKW'></ul>
              • 本文介绍了单击传单标记会将您带到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Leaflet 弹出窗口中添加按钮 下一篇:显示离线 OSM 映射文件.建议:一个带有 Js.library 的

                相关文章

                最新文章

                <legend id='DGSDt'><style id='DGSDt'><dir id='DGSDt'><q id='DGSDt'></q></dir></style></legend>

                <small id='DGSDt'></small><noframes id='DGSDt'>

                      <bdo id='DGSDt'></bdo><ul id='DGSDt'></ul>
                    <i id='DGSDt'><tr id='DGSDt'><dt id='DGSDt'><q id='DGSDt'><span id='DGSDt'><b id='DGSDt'><form id='DGSDt'><ins id='DGSDt'></ins><ul id='DGSDt'></ul><sub id='DGSDt'></sub></form><legend id='DGSDt'></legend><bdo id='DGSDt'><pre id='DGSDt'><center id='DGSDt'></center></pre></bdo></b><th id='DGSDt'></th></span></q></dt></tr></i><div id='DGSDt'><tfoot id='DGSDt'></tfoot><dl id='DGSDt'><fieldset id='DGSDt'></fieldset></dl></div>

                  1. <tfoot id='DGSDt'></tfoot>