![](/img/trans.png)
[英]leaflet plugin and leafletProxy with polylineDecorator as Example
[英]leaflet plugin and leafletProxy
類似於本示例,我已將傳單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) {
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)
})
根據文檔,我現在想使用leafletProxy()
以便每當反應性數據源發生更改時都不會重繪整個地圖。 ,,使用以下代碼片段
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)
不能按預期工作。 我認為這是因為onRender
僅在發生新渲染時才調用,並且不重新渲染leafletProxy
的全部內容嗎? 如果這是正確的,是否有辦法使用其他方法呢?
文檔說:影響renderLeaflet表達式的有效輸入和表達式將導致從頭重新繪制整個地圖,並重置地圖位置和縮放級別。
聽起來您將不得不自己深入研究Leaflet或觸發LeafletProxy,並通過使用兩個數據源來確保數據源沒有發生變化(也許“ Proxy”在這里用詞不當)。
看一下以下插件之一是否可以提供幫助: https : //leafletjs.com/plugins#dynamiccustom-data-loading
盡管可能會有更清潔的方法,但可能類似於以下內容。
我所做的就是用leafletProxy從添加折線層points_reactive()
函數,同時設置組要reactive
。 我曾經聽過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.