Розыгрыш листовок - toGeoJSON - PullRequest
0 голосов
/ 01 мая 2018

Я все еще новичок в Leaflet и учу себя. Я сделал карту с использованием библиотек Leaflet Draw, которая работает хорошо, и я хотел бы добавить функцию, которая позволяет пользователю сохранять нарисованные объекты в файле GeoJSON. Я пытался использовать это руководство , но мне все еще не повезло.

Я использую кнопку для функции экспорта:

    <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 или DrawItems, но я просто не могу понять это.

...