3

In a map built with Leaflet I created a button to export and download data as geoJSON drawn before by the user with Leaflet.draw.

To prevent an empty download, I am querying the existence of data.

In the leaflet drawing tool, the created data is stored in a layergroup named 'layerGroup'.

After loading the map for the first time, by clicking on the button I am getting the alert "There is nothing to download!", the console returns "Layers in featureGroup: 0" and no data was downloaded. So far so good.

Within the drawing tool I am able to add and remove data later again. After adding and then removing all data, there should be no layer in layerGroup and therefore I should experience the same acting like before with no download. In the console I get the message "Layers in featureGroup: 0", the alert "There is nothing to download!" is showing up, but, I don't know why, the download still starts.

Do you have any ideas why my code is acting like that?

That's my current attempt:

//Export Button 
document.getElementById('export').onclick = function(e) {
        //get layer number in var layer_number
        var layer_number = featureGroup.getLayers().filter( function(l) {return l instanceof L.Marker} ).length;
        console.log("Layers in featureGroup: ", layer_number);
        if (layer_number > 0){

            // 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',''+timestamp +'.geojson');

            //show confirmation popup       
            $('.confirmation').show();
            //hide popup on button click
            $('#agree_button').click(function(){
                $('.confirmation').hide();
            });
        }
        //meassage if no feature in featureGroup
        else {
            alert("There is nothing to download!");
        }           
    }
  • 1
    I postet that question in GIS-SE because, in my opinion, the average Leaflet.draw user is a geo-related user. For answering that question you need to have a little bit of knowledge how leaflet and its datamodel works, so the target group for potential answerers are located here and not in Stack Overflow. (Nevertheless I postet the same question there) – pascatl Feb 14 at 10:42
  • 1
    I can't see why this is off-topic. Leaflet for me is a clear GIS component, and as such on-topic as well as programming specific to it, as stated in the scope defined here: "Geographic Information Systems is a question and answer site for people interested in GIS, [...], as well as Programming/[...] specific to the aforementioned [...]." Further more this Q shows initial research effort. Voted to reopen. – Jochen Schwarze Feb 14 at 10:45
  • Did you try to debug code execution and set breakpoints within onclick function to check what is actually happening? – TomazicM Feb 14 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.