@@ -297,7 +297,8 @@ class GeoJson(Layer):
297297 >>> GeoJson(geojson, style_function=style_function)
298298 """
299299 def __init__ (self , data , style_function = None , name = None ,
300- overlay = True , control = True , smooth_factor = None ):
300+ overlay = True , control = True , smooth_factor = None ,
301+ highlight_function = None ):
301302 super (GeoJson , self ).__init__ (name = name , overlay = overlay ,
302303 control = control )
303304 self ._name = 'GeoJson'
@@ -333,16 +334,45 @@ def style_function(x):
333334 return {}
334335 self .style_function = style_function
335336
337+ self .highlight_function = highlight_function
338+
336339 self .smooth_factor = smooth_factor
337340
338341 self ._template = Template (u"""
339342 {% macro script(this, kwargs) %}
343+
344+ {% if this.highlight_function is not none %}
345+ {{this.get_name()}}_onEachFeature = function onEachFeature(feature, layer) {
346+ layer.on({
347+ mouseout: function(e) {
348+ e.target.setStyle(e.target.feature.properties.style);},
349+ mouseover: function(e) {
350+ e.target.setStyle(e.target.feature.properties.highlight);},
351+ click: function(e) {
352+ {{this._parent.get_name()}}.fitBounds(e.target.getBounds());}
353+ });
354+ };
355+ {% endif %}
356+
340357 var {{this.get_name()}} = L.geoJson(
341358 {% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %}
342- {% if this.smooth_factor is not none %}
343- , {smoothFactor:{{this.smooth_factor}}}
344- {% endif %}).addTo({{this._parent.get_name()}});
359+ {% if this.smooth_factor is not none or this.highlight_function is not none %}
360+ , {
361+ {% if this.smooth_factor is not none %}
362+ smoothFactor:{{this.smooth_factor}}}
363+ {% endif %}
364+
365+ {% if this.highlight_function is not none %}
366+ {% if this.smooth_factor is not none %}
367+ ,
368+ {% endif %}
369+ onEachFeature: {{this.get_name()}}_onEachFeature
370+ {% endif %}
371+ }
372+ {% endif %}
373+ ).addTo({{this._parent.get_name()}});
345374 {{this.get_name()}}.setStyle(function(feature) {return feature.properties.style;});
375+
346376 {% endmacro %}
347377 """ ) # noqa
348378
@@ -361,6 +391,7 @@ def style_data(self):
361391
362392 for feature in self .data ['features' ]:
363393 feature .setdefault ('properties' , {}).setdefault ('style' , {}).update (self .style_function (feature )) # noqa
394+ feature .setdefault ('properties' , {}).setdefault ('highlight' , {}).update (self .highlight_function (feature )) # noqa
364395 return json .dumps (self .data , sort_keys = True )
365396
366397 def _get_self_bounds (self ):
0 commit comments