I have made a map using the Leaflet Draw plugin which allows the user to download their drawn items. These drawnItems are exported as GeoJSON using the following code from here:

document.getElementById('export').onclick = function(e) {
// Extract GeoJSON from featureGroup
var data = featureGroup.toGeoJSON();

// Stringify the GeoJSON
var convertedData = 'text/json;charset=utf-8,' + 

// Create export
document.getElementById('export').setAttribute('href', 'data:' + convertedData);      

This works perfectly, but it would be even more ideal if the GeoJSON was converted to .kml before being exported. I am aware of the toKml plugin but I am struggling to get it to work (I am still quite new to all of this). Where would I add:

var kml = tokml(geojsonObject);

And I am sure I would have to edit the //create export part of the GeoJSON code

  • Cross posted on SO: stackoverflow.com/questions/50149464/… The answer there sounds reasonable. It seems unfair looking for help somewhere else without even providing some feedback to the person who tried to help you already.
    – ghybs
    May 6, 2018 at 13:05
  • Feedback provided on Stackoverflow. The answer was 99% correct, I just had to make one small change.
    – JohnGIS
    May 7, 2018 at 6:56

1 Answer 1

var data = featureGroup.toGeoJSON();
var kml = tokml(data);

var convertedData = 'application/xml;charset=utf-8,' + 

// if you want to use the official MIME type for KML
// var convertedData = 'application/vnd.google-earth.kml+xml;charset=utf-8,'+ 
// encodeURIComponent(kml);

document.getElementById('export').setAttribute('href', 'data:' + convertedData); 
document.getElementById('export').setAttribute('download', 'data.kml');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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