簡體   English   中英

Leaflet.Draw - geoJSON到Kml

[英]Leaflet.Draw - geoJSON to Kml

我使用Leaflet Draw插件創建了一個地圖,允許用戶下載他們繪制的項目。 這些繪制的項目使用從下面的代碼導出為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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM