Javascript 如何在用R语言制作的地图上添加下载按钮?

Javascript 如何在用R语言制作的地图上添加下载按钮?,javascript,r,dictionary,leaflet,Javascript,R,Dictionary,Leaflet,我尝试在Java中添加几个脚本来创建这个按钮,但它们都不起作用。你能帮我吗?我给你看我最后试过的东西 sp %>% leaflet() %>% addTiles() %>% addPolygons(label = ~lapply(paste0('<p>',"Ranking in ",'<b>',place,'</b>','</p>'),htmltools::HTML), smo

我尝试在Java中添加几个脚本来创建这个按钮,但它们都不起作用。你能帮我吗?我给你看我最后试过的东西

sp %>%  
        leaflet() %>%
        addTiles() %>%
        addPolygons(label = ~lapply(paste0('<p>',"Ranking in ",'<b>',place,'</b>','</p>'),htmltools::HTML),  smoothFactor = 0.3, fillOpacity = 0.7,opacity=1,
          fillColor = ~color,weight = 1, color = "#E9E9E9", highlightOptions = highlightOptions(color = "#FFFFFF", weight = 1, bringToFront = FALSE)) %>%
        addLegend(colors = col, title=paste("Ranking number  ",2),labels=la, opacity = 1.0, layerId="Download")%>%
        addPolygons(data=dissp, weight = 2, color = "#E9E9E9", fill=FALSE)%>%
addEasyButton(easyButton(  icon="fa-globe", title="Download",    onClick=JS("function(btn, map){
  var myLayer = L.geoJSON().addTo(map);
  var featureGroup=L.featureGroup().addTo(map);
  var data = featureGroup.toGeoJSON();
            // Stringify the GeoJson
            var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
            // Create export
            alert(convertedData)
            alert(featureGroup.getLayerId())
            document.getElementById('Download').setAttribute('href', 'data:' + convertedData);
            alert(map)
            document.getElementById('Download').setAttribute('download','data.geojson');
            alert(map)
    }")))
sp%>%
传单()%>%
addTiles()%>%
addPolygons(标签=~Lappy(粘贴0(“”,“排名在“,”,位置,,“

”),htmltools::HTML),smoothFactor=0.3,fillOpacity=0.7,opacity=1, fillColor=~color,weight=1,color=“#E9E9E9”,highlightOptions=highlightOptions(color=“#ffffffff”,weight=1,bringToFront=FALSE))%>% addLegend(颜色=颜色,标题=粘贴(“排名编号”,2),标签=la,不透明度=1.0,layerId=“下载”)%%>% 添加多边形(数据=dissp,权重=2,颜色=“#E9E9E9”,填充=FALSE)% addEasyButton(简易按钮)(icon=“fa globe”,title=“Download”,onClick=JS(“功能(btn,地图)){ var myLayer=L.geoJSON().addTo(map); var featureGroup=L.featureGroup().addTo(映射); var data=featureGroup.toGeoJSON(); //将GeoJson字符串化 var convertedData='text/json;charset=utf-8',+encodeURIComponent(json.stringify(data)); //创建导出 警报(转换数据) 警报(featureGroup.getLayerId()) document.getElementById('Download').setAttribute('href','data:'+convertedData); 警报(地图) document.getElementById('Download').setAttribute('Download','data.geojson'); 警报(地图) }")))
多谢各位

这适用于我(使用JQuery):

此外,我认为您的featureGroup没有数据。在调用
featuregroup.toGeoJSON()

这对我很有用(使用JQuery):

此外,我认为您的featureGroup没有数据。在调用
featuregroup.toGeoJSON()


最后,我做了一个闪亮的按钮,用来保存小部件。非常感谢。最后,我做了一个闪亮的按钮,用来保存小部件。非常感谢。
downloadObjectAsJson(data,"Test Name");

function downloadObjectAsJson(exportObj, exportName){
    var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
    var downloadAnchorNode = document.createElement('a');
    downloadAnchorNode.setAttribute("href",     dataStr);
    downloadAnchorNode.setAttribute("download", exportName + ".json");
    document.body.appendChild(downloadAnchorNode); // required for firefox
    downloadAnchorNode.click(); (JQuery part)
    downloadAnchorNode.remove(); (JQuery part)
}

L.marker([47.483047, 9.751740]).addTo(featureGroup);
var data = featureGroup.toGeoJSON()