在传单(R)中添加叠加图像时,如何添加组名



我想在R中的传单地图上叠加一个图像,并从R传单地图上的叠加图像中找到了一个很好的例子。以下是Andrew Reid的示例代码:


m <- leaflet() %>%
addTiles() %>%  # Add default OpenStreetMap map tiles
addMarkers(lng=-74.22655, lat=40.712216, popup="Bottom Right Corner") %>%
htmlwidgets::onRender("
function(el, x) {
console.log(this);
var myMap = this;
var imageUrl = 'http://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg';
var imageBounds = [[40.712216, -74.22655], [40.773941, -74.12544]];
L.imageOverlay(imageUrl, imageBounds).addTo(myMap);
}
")
m  # Print the map

我的问题是如何添加此图像层的组名,以便我可以在传单地图中显示/隐藏层,如1(https://rstudio.github.io/leaflet/showhide.html)。

我对这个问题的解决方案是使图像具有交互性,并在点击时更改其不透明度:

let vis = 0.95
let img = L.imageOverlay(imageUrl, imageBounds, {opacity:vis, interactive: true});
img.on('click', function(d) {
vis = 1-vis;
this.setOpacity(vis);
});
img.addTo(myMap)

最新更新