Leaflet Draw - toGeoJSON

时间:2018-05-01 13:28:19

标签: html leaflet geojson

我还是Leaflet的新手并且一直在教自己。我使用Leaflet Draw库制作了一个很好的地图,我想添加一个允许用户将绘制的特征保存到GeoJSON文件的函数。我尝试过使用this guide,但我仍然没有运气。

我正在使用导出功能按钮:

    <input type='button' id='export' value='Export Feature (.kml)' class='btn' />

使用以下正文:

var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
        osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> ',
        osm = L.tileLayer(osmUrl, { maxZoom: 18, attribution: osmAttrib }),
        map = new L.Map('map', { center: new L.LatLng(-29.5, 24.85), zoom: 6 }),
        drawnItems = L.featureGroup().addTo(map);
L.control.layers({
    'Street': osm.addTo(map),
    "Satellite": L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
        attribution: 'Tiles &copy; Esri</a>'
    })
}, {'Survey Area': drawnItems }, { position: 'topleft', collapsed: false }).addTo(map); 

    map.addControl(new L.Control.Draw({
    edit: {
        featureGroup: drawnItems,
        poly: {
            allowIntersection: false
        }
    },
    draw: {
        polygon: {
            allowIntersection: false,
            showArea: true
        }
    }
}));

map.on(L.Draw.Event.CREATED, function (event) {
    var layer = event.layer;

    drawnItems.addLayer(layer);
}); 

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

我认为问题出在featureGroup或drawnItems上,但我无法弄清楚。

0 个答案:

没有答案