Leaflet.Draw - geoJSON 到 Kml

Leaflet.Draw - geoJSON to Kml

我使用 Leaflet Draw 插件制作了一张地图,该插件允许用户下载他们绘制的项目。使用来自 here:

的以下代码将这些绘制的项目导出为 GeoJSON
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');
}

这很完美,但如果在导出之前将 GeoJSON 转换为 .kml 会更理想。我知道 toKml 插件,但我正在努力让它工作(我对这一切还是很陌生)。我要在哪里添加:

var kml = tokml(geojsonObject);

您可以使用 tokml(data)data 对象转换为 KML,并在数据 URL 中使用生成的字符串,并使用适当的 MIME 类型和文件名:

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

var convertedData = 'application/xml;charset=utf-8,' + encodeURIComponent(kml);

// 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');