简体   繁体   English

Leaflet插件和leafletProxy

[英]leaflet plugin and leafletProxy

I've added the leaflet TextPath plugin to a shiny app in analogy to this example . 类似于本示例,我已将传单TextPath插件添加到了闪亮的应用程序中。 This works pretty well: 效果很好:

output$fullscreen_map <- renderLeaflet({
  points <- points_reactive()

  points %>%
    leaflet() %>% 
    addTiles() %>%
    fitBounds(~min(lon), ~min(lat), ~max(lon), ~max(lat)) %>%
    registerPlugin(textPathPlugin) %>%
    onRender("function(el, x, data) {
                data = HTMLWidgets.dataframeToD3(data);
                data = data.map(function(val) { return [val.lat, val.lon]; });
                var layer = L.polyline(data);
                layer.on('mouseover', function () {
                this.setText('  ►  ', {repeat: true, attributes: {fill: 'blue'}});
                });
                layer.on('mouseout', function () {
                this.setText(null);
                });
                layer.addTo(this);
    }", data = points)

})

In accordance with the docs I would now like to use leafletProxy() so that the entire map doesn't get redrawn whenever the reactive data source changes. 根据文档,我现在想使用leafletProxy()以便每当反应性数据源发生更改时都不会重绘整个地图。 Alas, using the following snippet of code ,,使用以下代码片段

leafletProxy("fullscreen_map", data = points) %>%
  onRender("function(el, x, data) {
              data = HTMLWidgets.dataframeToD3(data);
              data = data.map(function(val) { return [val.lat, val.lon]; });
              var layer = L.polyline(data);
              layer.on('mouseover', function () {
              this.setText('  ►  ', {repeat: true, attributes: {fill: 'blue'}});
              });
              layer.on('mouseout', function () {
              this.setText(null);
              });
              layer.addTo(this);
  }", data = points)

does not work as intended. 不能按预期工作。 I assume this is because onRender is only called when a new render occurs and the whole point of leafletProxy is to not re-render? 我认为这是因为onRender仅在发生新渲染时才调用,并且不重新渲染leafletProxy的全部内容吗? If this is correct, is there a way to do this using other methods? 如果这是正确的,是否有办法使用其他方法呢?

The docs say: active inputs and expressions that affect the renderLeaflet expression will cause the entire map to be redrawn from scratch and reset the map position and zoom level. 文档说:影响renderLeaflet表达式的有效输入和表达式将导致从头重新绘制整个地图,并重置地图位置和缩放级别。

It sounds like you will have to dig deeper into Leaflet or trigger leafletProxy yourself, and make sure that the data source does not change, by using two data sources... (Perhaps "Proxy" is a misnomer here.) 听起来您将不得不自己深入研究Leaflet或触发LeafletProxy,并通过使用两个数据源来确保数据源没有发生变化(也许“ Proxy”在这里用词不当)。

Have a look and see if one of these plugins can help: https://leafletjs.com/plugins#dynamiccustom-data-loading 看一下以下插件之一是否可以提供帮助: https : //leafletjs.com/plugins#dynamiccustom-data-loading

It could be something like below, although there would be more cleaner methods. 尽管可能会有更清洁的方法,但可能类似于以下内容。

What I did was to use leafletProxy to add polylines layer from points_reactive() function, while setting group to be reactive . 我所做的就是用leafletProxy从添加折线层points_reactive()函数,同时设置组要reactive I used listened to layeradd event of map, and if a layer with reactive group got added, i added the textPath. 我曾经听过map的layeradd事件,如果添加了带有reactive组的图层,我就添加了textPath。

output$fullscreen_map <- renderLeaflet({
  points <- points_reactive()

  points %>%
    leaflet() %>% 
    addTiles() %>%
    fitBounds(~min(lon), ~min(lat), ~max(lon), ~max(lat)) %>%
    registerPlugin(textPathPlugin) %>%
    onRender("function(el, x, data) {
              var mymap = this;

              mymap.on('layeradd',
                function(e) {
                  console.log(e);
                  var layer = e.layer;

                  if (layer.groupname == 'reactive') {
                    layer.on('mouseover', function () {
                      this.setText('  ►  ', {repeat: true, attributes: {fill: 'blue'}});
                    });
                    layer.on('mouseout', function () {
                      this.setText(null);
                    });
                  }
                }
              );

            }", data = points)

  })

  observeEvent(input$clickme,{
               points <- points_reactive()

               leafletProxy('fullscreen_map') %>%
                 addPolylines(lng=points$lon, lat=points$lat, group='reactive')

  }
)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM