Skip to content

Commit b2f98cd

Browse files
committed
Add documentation
1 parent e677dbe commit b2f98cd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
```

0 commit comments

Comments
 (0)