Leaflet - Download drawn features on click

by edorap   Last Updated April 06, 2018 10:22 AM

I'm able to download features drawn on Leaflet using Dan Swick code:

document.getElementById('export').onclick = function(e) {
     // Extract GeoJson from featureGroup
     var data = featureGroup.toGeoJSON();
     // Stringify the GeoJson
     var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
     // Create export
     document.getElementById('export').setAttribute('href', 'data:' + convertedData);
     document.getElementById('export').setAttribute('download','data.geojson');
}

Now I'm trying to download them in a more elegant way using Leaflet.EasyButton:

L.easyButton({
    states: [{
        icon: '<img src="image.png">', 
        title: 'Export features to GeoJSON', 
        onClick: function(e) {
            // Extract GeoJson from featureGroup
            var data = featureGroup.toGeoJSON();
            // Stringify the GeoJson
            var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
            // Create export
            // NEED TO FIND A WAY TO DOWNLOAD 'convertedData' on button click
        }
    }]
}).addTo(map);

But in this way I'm not creating the element 'export' and I don't know how to start the download clicking on the button.

I already tried different approaches without any success.

Do you have any idea how to do it?

Thanks!



Related Questions


Leaflet features work on some browsers, but not all.

Updated September 10, 2017 13:22 PM

Placing easyButton outside Leaflet

Updated October 02, 2017 01:22 AM

error L is not defined

Updated March 08, 2018 13:22 PM

Displaying bounding box array on leaflet map

Updated February 20, 2017 13:22 PM

Leaflet draw export to kml

Updated November 08, 2017 15:22 PM