【问题标题】:Leaflet.Draw - geoJSON to KmlLeaflet.Draw - geoJSON 到 Kml
【发布时间】:2018-05-03 07:40:06
【问题描述】:

我使用 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);

【问题讨论】:

    标签: javascript leaflet kml geojson leaflet.draw


    【解决方案1】:

    您可以使用 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');
    

    【讨论】:

    • 谢谢@Nikoshr!我出错的部分绝对是正确的 MIME 类型。请注意,我必须更改“(数据);”到“(公里);”在 convertData 行的末尾,现在一切正常。
    • @JohnGIS 感谢您发现这一点
    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多