12
12
13
13
from folium .map import FitBounds
14
14
from folium .raster_layers import TileLayer
15
- from folium .utilities import _parse_size , _tmp_html , validate_location
15
+ from folium .utilities import (
16
+ _parse_size ,
17
+ _tmp_html ,
18
+ validate_location ,
19
+ parse_options ,
20
+ )
16
21
17
22
from jinja2 import Environment , PackageLoader , Template
18
23
@@ -93,23 +98,15 @@ class Map(MacroElement):
93
98
tiles: str, default 'OpenStreetMap'
94
99
Map tileset to use. Can choose from a list of built-in tiles,
95
100
pass a custom URL or pass `None` to create a map without tiles.
96
- API_key: str, default None
97
- API key for Cloudmade or Mapbox tiles.
101
+ For more advanced tile layer options, use the `TileLayer` class.
98
102
min_zoom: int, default 0
99
103
Minimum allowed zoom level for the tile layer that is created.
100
104
max_zoom: int, default 18
101
105
Maximum allowed zoom level for the tile layer that is created.
102
- max_native_zoom: int, default None
103
- The highest zoom level at which the tile server can provide tiles.
104
- If provided you can zoom in past this level. Else tiles will turn grey.
105
106
zoom_start: int, default 10
106
107
Initial zoom level for the map.
107
108
attr: string, default None
108
109
Map tile attribution; only required if passing custom tile URL.
109
- detect_retina: bool, default False
110
- If true and user is on a retina display, it will request four
111
- tiles of half the specified size and a bigger zoom level in place
112
- of one to utilize the high resolution.
113
110
crs : str, default 'EPSG3857'
114
111
Defines coordinate reference systems for projecting geographical points
115
112
into pixel (screen) coordinates and back.
@@ -140,20 +137,23 @@ class Map(MacroElement):
140
137
rare environments) even if they're supported.
141
138
zoom_control : bool, default True
142
139
Display zoom controls on the map.
140
+ **kwargs
141
+ Additional keyword arguments are passed to Leaflets Map class:
142
+ https://leafletjs.com/reference-1.4.0.html#map
143
143
144
144
Returns
145
145
-------
146
146
Folium Map Object
147
147
148
148
Examples
149
149
--------
150
- >>> map = folium.Map(location=[45.523, -122.675],
150
+ >>> m = folium.Map(location=[45.523, -122.675],
151
151
... width=750, height=500)
152
- >>> map = folium.Map(location=[45.523, -122.675],
152
+ >>> m = folium.Map(location=[45.523, -122.675],
153
153
tiles='Mapbox Control Room')
154
- >>> map = folium.Map(location=(45.523, -122.675), max_zoom=20,
154
+ >>> m = folium.Map(location=(45.523, -122.675), max_zoom=20,
155
155
tiles='Cloudmade', API_key='YourKey')
156
- >>> map = folium.Map(
156
+ >>> m = folium.Map(
157
157
... location=[45.523, -122.675],
158
158
... zoom_start=2,
159
159
... tiles='http://{s}.tiles.mapbox.com/v3/mapbox.control-room/{z}/{x}/{y}.png',
@@ -181,25 +181,14 @@ class Map(MacroElement):
181
181
{% endmacro %}
182
182
183
183
{% macro script(this, kwargs) %}
184
- {%- if this.max_bounds %}
185
- var bounds = L.latLngBounds(
186
- [{{ this.min_lat }}, {{ this.min_lon }}],
187
- [{{ this.max_lat }}, {{ this.max_lon }}]
188
- );
189
- {%- else %}
190
- var bounds = null;
191
- {%- endif %}
192
-
193
184
var {{ this.get_name() }} = L.map(
194
185
{{ this.get_name()|tojson }},
195
186
{
196
187
center: {{ this.location|tojson }},
197
- zoom: {{ this.zoom_start|tojson }},
198
- maxBounds: bounds,
199
- layers: [],
200
- worldCopyJump: {{ this.world_copy_jump|tojson }},
201
188
crs: L.CRS.{{ this.crs }},
202
- zoomControl: {{ this.zoom_control|tojson }},
189
+ {%- for key, value in this.options.items() %}
190
+ {{ key }}: {{ value|tojson }},
191
+ {%- endfor %}
203
192
}
204
193
);
205
194
@@ -220,15 +209,33 @@ class Map(MacroElement):
220
209
{% endmacro %}
221
210
""" )
222
211
223
- def __init__ (self , location = None , width = '100%' , height = '100%' ,
224
- left = '0%' , top = '0%' , position = 'relative' ,
225
- tiles = 'OpenStreetMap' , API_key = None , max_zoom = 18 , min_zoom = 0 ,
226
- max_native_zoom = None , zoom_start = 10 , world_copy_jump = False ,
227
- no_wrap = False , attr = None , min_lat = - 90 , max_lat = 90 ,
228
- min_lon = - 180 , max_lon = 180 , max_bounds = False ,
229
- detect_retina = False , crs = 'EPSG3857' , control_scale = False ,
230
- prefer_canvas = False , no_touch = False , disable_3d = False ,
231
- subdomains = 'abc' , png_enabled = False , zoom_control = True ):
212
+ def __init__ (
213
+ self ,
214
+ location = None ,
215
+ width = '100%' ,
216
+ height = '100%' ,
217
+ left = '0%' ,
218
+ top = '0%' ,
219
+ position = 'relative' ,
220
+ tiles = 'OpenStreetMap' ,
221
+ attr = None ,
222
+ min_zoom = 0 ,
223
+ max_zoom = 18 ,
224
+ zoom_start = 10 ,
225
+ min_lat = - 90 ,
226
+ max_lat = 90 ,
227
+ min_lon = - 180 ,
228
+ max_lon = 180 ,
229
+ max_bounds = False ,
230
+ crs = 'EPSG3857' ,
231
+ control_scale = False ,
232
+ prefer_canvas = False ,
233
+ no_touch = False ,
234
+ disable_3d = False ,
235
+ png_enabled = False ,
236
+ zoom_control = True ,
237
+ ** kwargs
238
+ ):
232
239
super (Map , self ).__init__ ()
233
240
self ._name = 'Map'
234
241
self ._env = ENV
@@ -239,10 +246,9 @@ def __init__(self, location=None, width='100%', height='100%',
239
246
if location is None :
240
247
# If location is not passed we center and zoom out.
241
248
self .location = [0 , 0 ]
242
- self . zoom_start = 1
249
+ zoom_start = 1
243
250
else :
244
251
self .location = validate_location (location )
245
- self .zoom_start = zoom_start
246
252
247
253
Figure ().add_child (self )
248
254
@@ -253,17 +259,18 @@ def __init__(self, location=None, width='100%', height='100%',
253
259
self .top = _parse_size (top )
254
260
self .position = position
255
261
256
- self .min_lat = min_lat
257
- self .max_lat = max_lat
258
- self .min_lon = min_lon
259
- self .max_lon = max_lon
260
- self .max_bounds = max_bounds
261
- self .no_wrap = no_wrap
262
- self .world_copy_jump = world_copy_jump
262
+ max_bounds_array = [[min_lat , min_lon ], [max_lat , max_lon ]] \
263
+ if max_bounds else None
263
264
264
265
self .crs = crs
265
266
self .control_scale = control_scale
266
- self .zoom_control = zoom_control
267
+
268
+ self .options = parse_options (
269
+ max_bounds = max_bounds_array ,
270
+ zoom = zoom_start ,
271
+ zoom_control = zoom_control ,
272
+ ** kwargs
273
+ )
267
274
268
275
self .global_switches = GlobalSwitches (
269
276
prefer_canvas ,
@@ -274,12 +281,9 @@ def __init__(self, location=None, width='100%', height='100%',
274
281
self .objects_to_stay_in_front = []
275
282
276
283
if tiles :
277
- self .add_tile_layer (
278
- tiles = tiles , min_zoom = min_zoom , max_zoom = max_zoom ,
279
- max_native_zoom = max_native_zoom , no_wrap = no_wrap , attr = attr ,
280
- API_key = API_key , detect_retina = detect_retina ,
281
- subdomains = subdomains
282
- )
284
+ tile_layer = TileLayer (tiles = tiles , attr = attr ,
285
+ min_zoom = min_zoom , max_zoom = max_zoom )
286
+ self .add_child (tile_layer , name = tile_layer .tile_name )
283
287
284
288
def _repr_html_ (self , ** kwargs ):
285
289
"""Displays the HTML Map in a Jupyter notebook."""
@@ -299,8 +303,8 @@ def _to_png(self, delay=3):
299
303
300
304
Examples
301
305
--------
302
- >>> map ._to_png()
303
- >>> map ._to_png(time=10) # Wait 10 seconds between render and snapshot.
306
+ >>> m ._to_png()
307
+ >>> m ._to_png(time=10) # Wait 10 seconds between render and snapshot.
304
308
305
309
"""
306
310
if self ._png_image is None :
@@ -329,24 +333,6 @@ def _repr_png_(self):
329
333
return None
330
334
return self ._to_png ()
331
335
332
- def add_tile_layer (self , tiles = 'OpenStreetMap' , name = None ,
333
- API_key = None , max_zoom = 18 , min_zoom = 0 ,
334
- max_native_zoom = None , attr = None , active = False ,
335
- detect_retina = False , no_wrap = False , subdomains = 'abc' ,
336
- ** kwargs ):
337
- """
338
- Add a tile layer to the map. See TileLayer for options.
339
-
340
- """
341
- tile_layer = TileLayer (tiles = tiles , name = name ,
342
- min_zoom = min_zoom , max_zoom = max_zoom ,
343
- max_native_zoom = max_native_zoom ,
344
- attr = attr , API_key = API_key ,
345
- detect_retina = detect_retina ,
346
- subdomains = subdomains ,
347
- no_wrap = no_wrap )
348
- self .add_child (tile_layer , name = tile_layer .tile_name )
349
-
350
336
def render (self , ** kwargs ):
351
337
"""Renders the HTML representation of the element."""
352
338
figure = self .get_root ()
@@ -408,7 +394,7 @@ def fit_bounds(self, bounds, padding_top_left=None,
408
394
409
395
Examples
410
396
--------
411
- >>> map .fit_bounds([[52.193636, -2.221575], [52.636878, -1.139759]])
397
+ >>> m .fit_bounds([[52.193636, -2.221575], [52.636878, -1.139759]])
412
398
413
399
"""
414
400
self .add_child (FitBounds (bounds ,
0 commit comments