(Canvas) Layer initialization code before tiles get drawn

82 views
Skip to first unread message

Maik Riechert

unread,
Jun 9, 2015, 11:57:20 AM6/9/15
to leafl...@googlegroups.com
Hi,

I use Leaflet since a couple of days and am really impressed by how easy it is to build nice maps with it. I use TileLayer.Canvas to draw scientific datasets. However I came across an issue where I don't know how to continue properly.

What I want is simple. When activating a layer (say, the "temperature" layer), I first need to fetch data which is needed by all the tiles (and their drawTile functions). However, I see no easy way to put this initialization code somewhere. Ideally, when a layer is activated, it would call a "layerInitialize" function which then asynchronously (as it needs to fetch data) would fire an event like "layerInitComplete" after which the actual tiles start drawing (that is, drawTile is called for each tile). Is something like that currently possible? If not, how should I do that?

Cheers
Maik

Rushforth, Peter

unread,
Jun 9, 2015, 12:52:50 PM6/9/15
to leafl...@googlegroups.com

Hi Maik,

 

Good question, I would like to hear the answer to that too J.  The way I’ve done something that sounds similar is to put the function call directly in the xhr processing callback.  When the callback is triggered, it calls the function to draw the data.  But perhaps there’s a bona fide event-driven way to do that.

 

Regards,

Peter

 

 

--

---
You received this message because you are subscribed to the Google Groups "Leaflet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-js+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maik Riechert

unread,
Jun 22, 2015, 12:22:06 PM6/22/15
to leafl...@googlegroups.com
So in the end it was quite simple...

The TileLayer class has a "onAdd" function which gets called when the layer is added to the map (before starting to draw tiles etc.). So what you have to do is basically....

var layer = L.tileLayer.canvas();
var originalOnAdd = layer.onAdd;
layer.onAdd = function (map) {
   // your init code...xhr...whatever
   originalOnAdd.call(layer, map); // can be called inside xhr callback
};

I'm not an expert in JS so I guess this can be made a bit nicer, but it works.

Cheers
Maik
Reply all
Reply to author
Forward
0 new messages