1

I tried adding several scripts in Java to create this button, but they have not worked. Could you help me? I show you the last thing I've tried.

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)
    }")))

Thank you very much!

1 Answer 1

0

This works for me (With JQuery):

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)
}

Also I think that your featureGroup has no data. You have to append first layers to the featuregroup before you can call featureGroup.toGeoJSON()

L.marker([47.483047, 9.751740]).addTo(featureGroup);
var data = featureGroup.toGeoJSON()
1
  • In the end I made a shiny button and used to savewidget. Thank you!
    – NoSe
    Commented Nov 26, 2019 at 10:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.