Skip to content

Commit 91f34f9

Browse files
authored
Merge pull request #1624 from Conengmo/pass-tilelayer-to-map
allow passing TileLayer to Map
2 parents 8da04d5 + d8ed353 commit 91f34f9

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

folium/folium.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ class Map(JSCSSMixin, MacroElement):
9797
Width of the map.
9898
height: pixel int or percentage string (default: '100%')
9999
Height of the map.
100-
tiles: str, default 'OpenStreetMap'
100+
tiles: str or TileLayer, default 'OpenStreetMap'
101101
Map tileset to use. Can choose from a list of built-in tiles,
102-
pass a custom URL or pass `None` to create a map without tiles.
102+
pass a custom URL, pass a TileLayer object,
103+
or pass `None` to create a map without tiles.
103104
For more advanced tile layer options, use the `TileLayer` class.
104105
min_zoom: int, default 0
105106
Minimum allowed zoom level for the tile layer that is created.
@@ -282,7 +283,9 @@ def __init__(
282283

283284
self.objects_to_stay_in_front = []
284285

285-
if tiles:
286+
if isinstance(tiles, TileLayer):
287+
self.add_child(tiles)
288+
elif tiles:
286289
tile_layer = TileLayer(tiles=tiles, attr=attr,
287290
min_zoom=min_zoom, max_zoom=max_zoom)
288291
self.add_child(tile_layer, name=tile_layer.tile_name)

tests/test_folium.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import branca.element
1111

1212
import folium
13+
from folium import TileLayer
1314
from folium.features import GeoJson, Choropleth
1415

1516
import jinja2
@@ -148,6 +149,13 @@ def test_custom_tile(self):
148149
bounds = m.get_bounds()
149150
assert bounds == [[None, None], [None, None]], bounds
150151

152+
def test_tilelayer_object(self):
153+
url = "http://{s}.custom_tiles.org/{z}/{x}/{y}.png"
154+
attr = "Attribution for custom tiles"
155+
m = folium.Map(location=[45.52, -122.67], tiles=TileLayer(url, attr=attr))
156+
assert next(iter(m._children.values())).tiles == url
157+
assert attr in m._parent.render()
158+
151159
def test_feature_group(self):
152160
"""Test FeatureGroup."""
153161

0 commit comments

Comments
 (0)