66Creates a marker shaped like a boat. Optionally you can append a wind direction.
77"""
88import json
9+ from jinja2 import Template
910
10- from . plugin import Plugin
11+ from folium . element import JavascriptLink , MacroElement , Figure
1112
12- class BoatMarker (Plugin ):
13+ class BoatMarker (MacroElement ):
1314 """Adds a BoatMarker layer on the map."""
1415 def __init__ (self , position = None , heading = 0 , wind_heading = None , wind_speed = 0 , ** kwargs ):
1516 """Creates a BoatMarker plugin to append into a map with
@@ -32,30 +33,28 @@ def __init__(self, position=None, heading=0, wind_heading=None, wind_speed=0, **
3233 Speed of the wind in knots.
3334 """
3435 super (BoatMarker , self ).__init__ ()
35- self .plugin_name = 'BoatMarker'
36+ self ._name = 'BoatMarker'
3637 self .position = None if position is None else tuple (position )
3738 self .heading = heading
3839 self .wind_heading = wind_heading
3940 self .wind_speed = wind_speed
40- self .kwargs = kwargs .copy ()
41-
42- def render_header (self , nb ):
43- """Generates the HTML part of the plugin."""
44- return """
45- <script src="https://thomasbrueggemann.github.io/leaflet.boatmarker/js/leaflet.boatmarker.min.js"></script>
46- """ if nb == 0 else ""
47-
48- def render_js (self , nb ):
49- """Generates the Javascript part of the plugin."""
50- kwargs_str = "{%s}" % "," .join (["%s : %s" % (key ,json .dumps (val )) for (key ,val ) in self .kwargs .items ()])
51- position_str = "map.getCenter()" if self .position is None else "[%.12f,%.12f]" % self .position
52- out = 'var boatMarker_%s = L.boatMarker(%s, %s).addTo(map);' % (nb ,position_str ,kwargs_str )
53-
54- if self .wind_heading is None :
55- out += "boatMarker_%s.setHeading(%s);" % (nb ,int (self .heading ))
56- else :
57- out += "boatMarker_%s.setHeadingWind(%s, %s, %s);" % (nb ,int (self .heading ),
58- int (self .wind_speed ),
59- int (self .wind_heading ),
60- )
61- return out
41+ self .kwargs = json .dumps (kwargs )
42+
43+ self ._template = Template (u"""
44+ {% macro script(this, kwargs) %}
45+ var {{this.get_name()}} = L.boatMarker(
46+ [{{this.position[0]}},{{this.position[1]}}],
47+ {{this.kwargs}}).addTo({{this._parent.get_name()}});
48+ {{this.get_name()}}.setHeadingWind({{this.heading}}, {{this.wind_speed}}, {{this.wind_heading}});
49+ {% endmacro %}
50+ """ )
51+ def render (self ,** kwargs ):
52+ super (BoatMarker ,self ).render (** kwargs )
53+
54+ figure = self .get_root ()
55+ assert isinstance (figure ,Figure ), ("You cannot render this Element "
56+ "if it's not in a Figure." )
57+
58+ figure .header .add_children (\
59+ JavascriptLink ("https://thomasbrueggemann.github.io/leaflet.boatmarker/js/leaflet.boatmarker.min.js" ),
60+ name = 'markerclusterjs' )
0 commit comments