在Python Leaflet / folium Maps中,有没有一种方法可以进行搜索,从而打开相关标记的弹出窗口而不是突出显示?

时间:2019-05-01 11:00:08

标签: python leaflet folium

我在Python中使用Folium / Leaflet绘制点标记。

问题是我无法将弹出窗口显示与搜索结果链接。

现在搜索结果仅用红色圆圈突出显示标记。我希望打开并显示相关的标记弹出窗口。

我该怎么做?

enter image description here

要创建弹出窗口并进行搜索,我必须使用两种不同类型的标记-

  

folium.Marker(因为它可以包含h​​tml内容)

     

folium.GeoJson(因为可以基于此创建搜索)

图片:

这是代码:

######################################################################
# Part 1 - Creating map

m_search = folium.Map(location=[28.6003435, 77.21952300000001],zoom_start=11
              )   

#######################################################################
# Part 2 - Creating folium markers on map with html text, images 
# etc dynamically for multiple points using for-loop


for plot_numb in range(gdf.shape[0]):

    icon = folium.Icon(color="blue", icon="cloud", icon_color='white')
    tooltip = 'Click to view more about: '+gdf.iloc[plot_numb,0]

    var_name = gdf.iloc[plot_numb,0] 
    var_loc = gdf.iloc[plot_numb,2] 
    pic = base64.b64encode(open('Images/'+gdf.iloc[plot_numb,5],'rb').read()).decode()    

    html = f'''<img ALIGN="Right" src="data:image/png;base64,{pic}">\
    <strong>Name: </strong>{var_name}<br /><br />\
    <strong>Location: </strong>{var_loc}<br /><br />\
    '''
    html 

    iframe = IFrame(html, width=300+180, height=300)
    popup = folium.Popup(iframe, max_width=650) 

    marker = folium.Marker(location=gdf.iloc[plot_numb,1], popup=popup, tooltip=tooltip, icon=icon).add_to(m_search)


########################################################################
# Part 3 - Creating Markers using GeoJson, creating search  

# creating folium GeoJson objects from out GeoDataFrames
pointgeo = folium.GeoJson(gdf,name='group on map', show=False,
                      tooltip=folium.GeoJsonTooltip(fields=['Name', 'Relation', 'City'], aliases=['Name','Relation', 'City'],
                        localize=True)).add_to(m_search)

# Add some Search boxes to the map that reference the GeoDataFrames with some different parameters passed to the 
# arguments.

pointsearch = Search(layer=pointgeo, geom_type='Point', 
                 placeholder='Search for contacts', collapsed=False,
                 search_label='Name').add_to(m_search)


# To Add a LayerControl add below line
folium.LayerControl().add_to(m_search)

m_search 

下面是gdf数据框的示例:

    Name    Location    City    Relation    Relation Detail Image   Lat Lon geometry
0   abc [28.562193, 77.387073]  Noida   Cousin  Cousin  1.png   28.562193   77.387073   POINT (77.387073 28.562193)
1   def [28.565282027743955, 77.44913935661314] Noida   Cousin  Cousin Brother  2.png   28.565282   77.449139   POINT (77.44913935661314 28.56528202774395)
3   ghi [28.6206996683473, 77.42576122283936]   Noida   Cousin  Cousin Brother  4.png   28.620700   77.425761   POINT (77.42576122283936 28.6206996683473)

0 个答案:

没有答案