【问题标题】:Python folium - insert class inside df.iterrowsPython folium - 在 df.iterrows 中插入类
【发布时间】:2023-01-18 01:40:44
【问题描述】:

我想在 MacroElement 类中执行我的 df.iterrows 循环的内容。我的代码如下所示:

for i,row in df.iterrows():
lat =df.at[i, 'lat']
lng = df.at[i, 'lng']
sp = df.at[i, 'sp']
phone = df.at[i, 'phone']
role = df.at[i, 'role']
rad = int(df.at[i, 'radius'])

class Circle(MacroElement):
_template = Template(u"""
        {% macro script(this, kwargs) %}
            var circle_job = L.circle();
            function newCircle(e){
                circle_job.setLatLng(e.latlng).addTo({{this._parent.get_name()}});
                circle_job.setRadius(50000);
                circle_job.setStyle({
                color: 'black',
                fillcolor: 'black'
                });
                };
            {{this._parent.get_name()}}.on('click', newCircle);      
        {% endmacro %}
        """)  # noqa

  def __init__(self,
             popup=None
             ):
    super(Circle, self).__init__()
    self._name = 'Circle'

 job_range = Circle()

popup = '<b>Phone: </b>' + str(df.at[i,'phone'])

if role == 'Contractor':
    fs.add_child(
        folium.Marker(location=[lat,lng],
                  tooltip=folium.map.Tooltip(
                      text='<strong>Contact surveyor</strong>',
                      style=("background-color: lightgreen;")),
                  popup=popup,
                  icon = folium.Icon(color='darkred', icon='glyphicon-user'
                                     )
                )
    )
    fs.add_child (
        folium.Marker(location=[lat,lng],
                  popup=popup,
                  icon = folium.DivIcon(html="<b>" + sp + "</b>",
                                        class_name="mapText_contractor",
                                        icon_anchor=(30,5))
                  #click_action = js_f
                )
    )
    fs.add_child(job_range)

但它不起作用。我的地图上缺少一些功能,而 JavaScript 控制台什么也没说。

有没有办法在 df.iterrow 循环中包含 Python 类?

【问题讨论】:

    标签: python folium


    【解决方案1】:

    代替:

    fs.add_child(
            folium.Marker(...
    

    add_to() 用于fs

    folium.Marker(location=[lat,lng],
                      tooltip=folium.map.Tooltip(
                          text='<strong>Contact surveyor</strong>',
                          style=("background-color: lightgreen;")),
                      popup=popup,
                      icon = folium.Icon(color='darkred', icon='glyphicon-user'
                                         )
                    )
        ).add_to(fs)
    

    【讨论】:

      猜你喜欢
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 2022-11-10
      • 2023-01-21
      相关资源
      最近更新 更多