1

Can't find proper event for vector layer like on "load", to be fired when this layer is fuly rendered. I add vector features to a layer in Leaflet using proj4leaflet from JSON object like this

   var geojsonLayer = L.Proj.geoJson(features, {
        'pointToLayer': function (feature, latlng) {
            return L.marker(latlng);
        }
    }).addTo(map);

And this methid does not work

geojsonLayer.on("load", function () {
        console.log(1);
    });

I need a loadend or renderend event for this layer. Is there any?

2 Answers 2

1

As far as I know there is no loadend-event included in Leaflet for GeoJSON-layers.

https://github.com/Leaflet/Leaflet/issues/366

(Comment of mourner: "There is load event for all asynchronous layers (TileLayer and ImageOverlay). GeoJSON doesn't need that as it accepts already loaded JSON data, and loading logic happens externally." )

You could have a look at leaflet-ajax which will give you three events:

data:loading, data:progress and data:loaded.

https://github.com/calvinmetcalf/leaflet-ajax

This works nicely together with Leaflet.Spin:

https://github.com/makinacorpus/Leaflet.Spin

Here you can find a jsfiddle i set up:

http://jsfiddle.net/expedio/ea7esh0t/

Perhaps you can switch from L.Proj.geoJson to L.geoJson.ajax (reproject the data before you publish it?) or have a look at how these events were implemented so you could use them in L.Proj.geoJson,too.

0

When using Leaflet-Ajax you can do:

//setup loading
geojsonLayer = new L.GeoJSON.AJAX(objJson, {style: style, onEachFeature: onEachFeature});
//wait to hit the function
geojsonLayer.on('data:loaded',function(e){
    alert("loaded");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.