28
28
parse_options ,
29
29
camelize
30
30
)
31
- from folium .vector_layers import PolyLine , path_options
31
+ from folium .vector_layers import Circle , CircleMarker , PolyLine , path_options
32
32
33
33
from jinja2 import Template
34
34
@@ -346,6 +346,12 @@ class GeoJson(Layer):
346
346
tooltip: GeoJsonTooltip, Tooltip or str, default None
347
347
Display a text when hovering over the object. Can utilize the data,
348
348
see folium.GeoJsonTooltip for info on how to do that.
349
+ popup: GeoJsonPopup, optional
350
+ Show a different popup for each feature by passing a GeoJsonPopup object.
351
+ marker: Circle, CircleMarker or Marker, optional
352
+ If your data contains Point geometry, you can format the markers by passing a Cirle,
353
+ CircleMarker or Marker object with your wanted options. The `style_function` and
354
+ `highlight_function` will also target the marker object you passed.
349
355
embed: bool, default True
350
356
Whether to embed the data in the html file or not. Note that disabling
351
357
embedding is only supported if you provide a file link or URL.
@@ -396,21 +402,48 @@ class GeoJson(Layer):
396
402
}
397
403
}
398
404
{%- endif %}
405
+
406
+ {%- if this.marker %}
407
+ function {{ this.get_name() }}_pointToLayer(feature, latlng) {
408
+ var opts = {{ this.marker.options | tojson | safe }};
409
+ {% if this.marker._name == 'Marker' and this.marker.icon %}
410
+ const iconOptions = {{ this.marker.icon.options | tojson | safe }}
411
+ const iconRootAlias = L{%- if this.marker.icon._name == "Icon" %}.AwesomeMarkers{%- endif %}
412
+ opts.icon = new iconRootAlias.{{ this.marker.icon._name }}(iconOptions)
413
+ {% endif %}
414
+ {%- if this.style_function %}
415
+ let style = {{ this.get_name()}}_styler(feature)
416
+ Object.assign({%- if this.marker.icon -%}opts.icon.options{%- else -%} opts {%- endif -%}, style)
417
+ {% endif %}
418
+ return new L.{{this.marker._name}}(latlng, opts)
419
+ }
420
+ {%- endif %}
421
+
399
422
function {{this.get_name()}}_onEachFeature(feature, layer) {
400
423
layer.on({
401
424
{%- if this.highlight %}
402
425
mouseout: function(e) {
403
- {{ this.get_name() }}.resetStyle(e.target);
426
+ if(typeof e.target.setStyle === "function"){
427
+ {{ this.get_name() }}.resetStyle(e.target);
428
+ }
404
429
},
405
430
mouseover: function(e) {
406
- e.target.setStyle({{ this.get_name() }}_highlighter(e.target.feature));
431
+ if(typeof e.target.setStyle === "function"){
432
+ const highlightStyle = {{ this.get_name() }}_highlighter(e.target.feature)
433
+ e.target.setStyle(highlightStyle);
434
+ }
407
435
},
408
436
{%- endif %}
409
437
{%- if this.zoom_on_click %}
410
438
click: function(e) {
411
439
if (typeof e.target.getBounds === 'function') {
412
440
{{ this.parent_map.get_name() }}.fitBounds(e.target.getBounds());
413
441
}
442
+ else if (typeof e.target.getLatLng === 'function'){
443
+ let zoom = {{ this.parent_map.get_name() }}.getZoom()
444
+ zoom = zoom > 12 ? zoom : zoom + 1
445
+ {{ this.parent_map.get_name() }}.flyTo(e.target.getLatLng(), zoom)
446
+ }
414
447
}
415
448
{%- endif %}
416
449
});
@@ -423,6 +456,9 @@ class GeoJson(Layer):
423
456
{% if this.style %}
424
457
style: {{ this.get_name() }}_styler,
425
458
{%- endif %}
459
+ {%- if this.marker %}
460
+ pointToLayer: {{ this.get_name() }}_pointToLayer
461
+ {%- endif %}
426
462
});
427
463
428
464
function {{ this.get_name() }}_add (data) {
@@ -443,7 +479,7 @@ class GeoJson(Layer):
443
479
def __init__ (self , data , style_function = None , highlight_function = None , # noqa
444
480
name = None , overlay = True , control = True , show = True ,
445
481
smooth_factor = None , tooltip = None , embed = True , popup = None ,
446
- zoom_on_click = False ):
482
+ zoom_on_click = False , marker = None ):
447
483
super (GeoJson , self ).__init__ (name = name , overlay = overlay ,
448
484
control = control , show = show )
449
485
self ._name = 'GeoJson'
@@ -455,6 +491,10 @@ def __init__(self, data, style_function=None, highlight_function=None, # noqa
455
491
self .style = style_function is not None
456
492
self .highlight = highlight_function is not None
457
493
self .zoom_on_click = zoom_on_click
494
+ if marker :
495
+ if not isinstance (marker , (Circle , CircleMarker , Marker )):
496
+ raise TypeError ("Only Marker, Circle, and CircleMarker are supported as GeoJson marker types." )
497
+ self .marker = marker
458
498
459
499
self .data = self .process_data (data )
460
500
0 commit comments