如何知道 leaflet 图层在浏览器中是否完全渲染?

huangapple go评论122阅读模式
英文:

How to know, whether leaflet layer is fully rendered or not in browser?

问题

我试图检查图层渲染事件。我尝试了以下代码,但它不起作用。我漏掉了什么吗?

var renderer = new L.canvas(); // 我也尝试了 L.svg()
var latlngs = [[37, -109.05], [41, -109.03], [41, -102.05], [37, -102.04]];
var polygon = L.polygon(latlngs, { color: 'red', renderer: renderer }).addTo(map);

// 更新事件
renderer.on('update', () => {
      console.log('多边形已加载')
      renderer.off('update')
    })

我注意到渲染器上的更新方法只在地图平移或缩放事件之后才起作用。但在第一次地图渲染时,它没有触发。请帮忙,谢谢。

我已经在 leaflet讨论区中提出了同样的问题。

PS:以下代码对于 tilelayer 完美运行,但对于其他类型的图层则不起作用。

tile_layer.on("load", function() { console.log("所有可见瓦片已加载") });
英文:

I am trying to check the layer rendering event. I have tried the following code, but it is not working. Am I missing something?

var renderer = new L.canvas(); // I tried with L.svg() too
var latlngs = [[37, -109.05], [41, -109.03], [41, -102.05], [37, -102.04]];
var polygon = L.polygon(latlngs, { color: 'red', renderer: renderer }).addTo(map);

//update event
renderer.on('update', () => {
      console.log('polygon loaded')
      renderer.off('update')
    })

I notice that the update method on the renderer only works after a map pan or zoom event. But on the first map render, it didn't trigger. Any help, please?

> I have added the same question in leaflet discussion section

PS: The following code works perfectly for the tilelayer but not on the other type of layers.

tile_layer.on("load",function() { console.log("all visible tiles have been loaded") });

答案1

得分: 3

只需更改您的代码顺序,特别是这些行。

renderer.on('update', () => {
    console.log('多边形已加载');
    renderer.off('update');
});
var polygon = L.polygon(latlngs, { color: '红色', renderer: renderer }).addTo(map);
英文:

just change the order of your code.Specially these lines.<br>

renderer.on(&#39;update&#39;, () =&gt; {
    console.log(&#39;polygon loaded&#39;)
    renderer.off(&#39;update&#39;)
})
var polygon = L.polygon(latlngs, { color: &#39;red&#39;, renderer: renderer }).addTo(map);

huangapple
  • 本文由 发表于 2023年6月12日 23:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定