最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Callback when all layer are added to leafletjs map - Stack Overflow

programmeradmin4浏览0评论

Situation: I am adding a new geoJson layer to my map and I want to animate all the markers in once they have been added with a slight delay between each one. The API appears to do everything BUT offer a callback for when the last marker is added!

Sample code

L.geoJson(data, {      
    onEachFeature: function (feature, layer) {
        console.log(feature, layer);
    }
}).addTo(map);

what I would like is

 L.geoJson(data, {      
    onEachFeature: function (feature, layer) {
        console.log(feature, layer);
    },
    plete:function(layers){
        console.log(layers);
    }
}).addTo(map);

I know each layer has an onAdd event but is there similar for a layerGroup?

Thanks

Situation: I am adding a new geoJson layer to my map and I want to animate all the markers in once they have been added with a slight delay between each one. The API appears to do everything BUT offer a callback for when the last marker is added!

Sample code

L.geoJson(data, {      
    onEachFeature: function (feature, layer) {
        console.log(feature, layer);
    }
}).addTo(map);

what I would like is

 L.geoJson(data, {      
    onEachFeature: function (feature, layer) {
        console.log(feature, layer);
    },
    plete:function(layers){
        console.log(layers);
    }
}).addTo(map);

I know each layer has an onAdd event but is there similar for a layerGroup?

Thanks

Share Improve this question asked Sep 19, 2013 at 14:54 sidonaldsonsidonaldson 25.3k10 gold badges60 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I have got in contact with the major contributors of leafletjs and there is no need to have a callback for a standard geojson layer, you can just do this;

var layer = L.geoJson(data, {}).addTo(map);
animateMyIconsIn(layer.getLayers()); // your custom function

However you do need a callback if you are using the AJAX plugin;

//first create the layer
var  layer = L.geoJson.AJAX(url, {});
//then add a listener
layer.on('dataLoadComplete',function(e){
    //finally add the layer
    layer.addTo(map);
    animateMyIconsIn(layer.getLayers()); // your custom function
});

updated syntax is currently "data: loaded" as found from this post;

https://gis.stackexchange./questions/271919/how-to-cluster-external-geojson-using-leaflet-ajax-js

发布评论

评论列表(0)

  1. 暂无评论