File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ # Overriding Leaflet class methods
2+
3+ ``` {code-cell} ipython3
4+ ---
5+ nbsphinx: hidden
6+ ---
7+ import folium
8+ ```
9+
10+ ## Customizing Leaflet behavior
11+ Sometimes you want to override Leaflet's javascript behavior. This can be done using the ` Class.include ` statement. This mimics Leaflet's
12+ ` L.Class.include ` method. See [ here] ( https://leafletjs.com/examples/extending/extending-1-classes.html ) for more details.
13+
14+ ### Example: adding an authentication header to a TileLayer
15+ One such use case is if you need to override the ` createTile ` on ` L.TileLayer ` , because your tiles are hosted on an oauth2 protected
16+ server. This can be done like this:
17+
18+ ``` {code-cell}
19+ create_tile = JsCode("""
20+ function(coords, done) {
21+ const url = this.getTileUrl(coords);
22+ const img = document.createElement('img');
23+ fetch(url, {
24+ headers: {
25+ "Authorization": "Bearer <Token>"
26+ },
27+ })
28+ .then((response) => {
29+ img.src = URL.createObjectURL(response.body);
30+ done(null, img);
31+ })
32+ return img;
33+ }
34+ """)
35+
36+ folium.TileLayer.include(create_tile=create_tile)
37+ tiles = folium.TileLayer(
38+ tiles="OpenStreetMap",
39+ )
40+ m = folium.Map(
41+ tiles=tiles,
42+ )
43+
44+
45+ m = folium.Map()
46+ ```
You can’t perform that action at this time.
0 commit comments