简体   繁体   English

Python folium Clickformarker - 2 个功能不相互协作

[英]Python folium Clickformarker - 2 functions don't collaborate with each other

I would like to use the folium.CLickFormarker macro more than once in my map. Unfortunately it doesn't work, as my function takes only the first one.我想在我的 map 中多次使用 folium.CLickFormarker 宏。不幸的是它不起作用,因为我的 function 只需要第一个。

fs=folium.FeatureGroup(name="Surveyors")

df = pd.read_csv("survey.csv")

class Circle(folium.ClickForMarker):
_template = Template(u"""
        {% macro script(this, kwargs) %}
            var circle_job = L.circle();
            function newMarker(e){
                circle_job.setLatLng(e.latlng).addTo({{this._parent.get_name()}});
                circle_job.setRadius(50000);
                circle_job.bringToFront();
                };
            {{this._parent.get_name()}}.on('click', newMarker);      
        {% endmacro %}
        """)  # noqa

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

 job_range2 = Circle()


 class ClickForOneMarker(folium.ClickForMarker):
   _template = Template(u"""
        {% macro script(this, kwargs) %}
            var new_mark = L.marker();
            function newMarker(e){
                new_mark.setLatLng(e.latlng).addTo({{this._parent.get_name()}});
                new_mark.setZIndexOffset(-1);
                new_mark.on('dblclick', function(e){
                {{this._parent.get_name()}}.removeLayer(e.target)})
                var lat = e.latlng.lat.toFixed(4),
                   lng = e.latlng.lng.toFixed(4);
                new_mark.bindPopup("<a href=https://www.google.com/maps?layer=c&cbll=" + lat + "," + lng + " target=blank><img src=img/streetview.svg width=150 title=StreetView></img></a>")//.openPopup();
                };
            {{this._parent.get_name()}}.on('click', newMarker);      
        {% endmacro %}
        """)  # noqa

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


click_for_marker = ClickForOneMarker()

map.add_child(click_for_marker)

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'])

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

order_rng = folium.Circle([lat,lng],
        radius=rad * 10.560,           
        popup= df.at[i, 'sp'],
        tooltip = sp + ' - Job limits',
        color='black',
        fill=True,
        fill_color='black',
        opacity=0.1,
        fill_opacity=0.1
      )

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))
                )
    )
    fs.add_child(job_range)

The first one is just what I want to include as the child for the existing feature group.第一个就是我想作为现有功能组的子项包含的内容。 The second one should be applicable to the entire map. Both don't work when included together.第二个应该适用于整个 map。两者放在一起时不起作用。 Is it folium limited to one ClickForMarker macro or something?它是否仅限于一个 ClickForMarker 宏或其他内容?

The primary error was using the same JavaScript function newMarker(e) , which created the conflict.主要错误是使用相同的 JavaScript function newMarker(e) ,这造成了冲突。 I've provided the other function for my second case and it has started working correctly.我已经为我的第二个案例提供了另一个 function,它已经开始正常工作了。

class ClickForOneMarker(folium.ClickForMarker): _template = Template(u""" {% macro script(this, kwargs) %} var new_mark = L.marker(); function newMarker(e){ new_mark.setLatLng(e.latlng).addTo({{this._parent.get_name()}}); new_mark.setZIndexOffset(-1); new_mark.on('dblclick', function(e){ {{this._parent.get_name()}}.removeLayer(e.target)}) var lat = e.latlng.lat.toFixed(4), lng = e.latlng.lng.toFixed(4); new_mark.bindPopup("<a href=https://www.google.com/maps?layer=c&cbll=" + lat + "," + lng + " target=blank>")//.openPopup(); }; {{this._parent.get_name()}}.on('click', newMarker); class ClickForOneMarker(folium.ClickForMarker): _template = Template(u""" {% macro script(this, kwargs) %} var new_mark = L.marker(); function newMarker(e){ new_mark.setLatLng(e.latlng) .addTo({{this._parent.get_name()}}); new_mark.setZIndexOffset(-1); new_mark.on('dblclick', function(e){{this._parent.get_name()}}.removeLayer (e.target)}) var lat = e.latlng.lat.toFixed(4), lng = e.latlng.lng.toFixed(4); new_mark.bindPopup("<a href=https://www.google .com/maps?layer=c&cbll=" + lat + "," + lng + " target=blank>")//.openPopup(); }; {{this._parent.get_name()}}.on('点击', newMarker);
{% endmacro %} """) # noqa {% endmacro %} """) # noqa

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


click_for_marker = ClickForOneMarker()

map.add_child(click_for_marker)

class Circle(folium.ClickForMarker): _template = Template(u""" {% macro script(this, kwargs) %} var circle_job = L.circle(); function newCircle(e){ class Circle(folium.ClickForMarker): _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.bringToFront();
                };
            {{this._parent.get_name()}}.on('click', newCircle);      
        {% endmacro %}
        """)  # noqa

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

job_range = Circle()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM