Folium 搜索插件搜索弹出文本

Folium Search Plugin Does Search Popup Text

提问人:DylanK8 提问时间:11/13/2023 最后编辑:Ken WhiteDylanK8 更新时间:11/13/2023 访问量:23

问:

我已经让 Folium 搜索框插件适用于代码 2,但是,由于我想要使用圆形标记,而不必放大即可查看分组标记,因此代码 1 是我试图修复的问题。搜索框似乎不想搜索站点 ID 或邮政编码(显示“找不到位置”),这与代码 2 不同,它运行良好。任何帮助都是值得赞赏的

代码 1

import folium
from folium.plugins import Search
import pandas as pd
from geopy.geocoders import Nominatim
from datetime import datetime

excel_file_path = r'test.xlsx'

columns_to_read = ['Status', 'Updates', 'Site ID', 'Site Name', 'Address 1', 'Postcode', 'Site Tier Classification', 'Circuit Handover Date']
df = pd.read_excel(excel_file_path, sheet_name='Tracker', engine='openpyxl', usecols=columns_to_read)
df['Site ID'] = df['Site ID'].astype(str).str.rstrip('.0')

def get_coordinates(postcode):
    geolocator = Nominatim(user_agent="postcode_mapper")
    location = geolocator.geocode(postcode)
    return (location.latitude, location.longitude) if location else None

def visualize_locations_with_circle_markers(df, zoom=6):
    center_lat, center_lon = 54.7023545, -3.2765753  

    uk_map = folium.Map([center_lat, center_lon], zoom_start=zoom)

    feature_group = folium.FeatureGroup(name='Locations').add_to(uk_map)

    for _, row in df.iterrows():
        coordinates = get_coordinates(row['Postcode'])

        if coordinates:
            pin_date = row['Circuit Handover Date']
            if pd.isnull(pin_date):
                pin_color = 'grey'
            else:
                if isinstance(pin_date, pd.Timestamp):
                    pin_date = pin_date.date()

                pin_color = 'red' if pin_date > datetime.today().date() else 'green'

            html_table = df.loc[[row.name]].to_html(classes="table table-striped table-hover table-condensed table-responsive", index=False)

            popup_content = f"<b>Site ID:</b> {row['Site ID']}<br><b>Postcode:</b> {row['Postcode']}<br><b>Address:</b> {row['Address 1']}<br>{html_table}"

            folium.CircleMarker(
                location=coordinates,
                radius=5,
                color=pin_color,
                fill=True,
                fill_color=pin_color,
                fill_opacity=1.0,
                popup=folium.Popup(popup_content, max_width=800),
                siteIDsearch=row['Site ID'],  
                postcodesearch=row['Postcode']  
            ).add_to(feature_group)

    uk_map.add_child(feature_group)

    servicesearch = Search(
        layer=feature_group,
        search_label="siteIDsearch", 
        placeholder='Search for a Site ID',
        collapsed=False,
    ).add_to(uk_map)

    postcodesearch = Search(
        layer=feature_group,
        search_label="postcodesearch",  
        placeholder='Search for a Postcode',
        collapsed=False,
    ).add_to(uk_map)

    return uk_map

visualize_locations_with_circle_markers(df).save("map_with_search_and_circles.html")
print("Map saved as HTML.")

代码 2

import folium
from folium.plugins import MarkerCluster, Search
import pandas as pd
from geopy.geocoders import Nominatim
from datetime import datetime

excel_file_path = r'test.xlsx'

columns_to_read = ['Status', 'Updates', 'Site ID', 'Site Name', 'Address 1', 'Postcode', 'Site Tier Classification', 'Circuit Handover Date']
df = pd.read_excel(excel_file_path, sheet_name='Tracker', engine='openpyxl', usecols=columns_to_read)
df['Site ID'] = df['Site ID'].astype(str).str.rstrip('.0')

def get_coordinates(postcode):
    geolocator = Nominatim(user_agent="postcode_mapper")
    location = geolocator.geocode(postcode)
    return (location.latitude, location.longitude) if location else None

def visualize_locations_with_cluster_markers(df, zoom=6):
    center_lat, center_lon = 54.7023545, -3.2765753  

    uk_map = folium.Map([center_lat, center_lon], zoom_start=zoom)

    marker_cluster = MarkerCluster(name='Locations').add_to(uk_map)

    for _, row in df.iterrows():
        coordinates = get_coordinates(row['Postcode'])

        if coordinates:
            pin_date = row['Circuit Handover Date']
            if pd.isnull(pin_date):
                pin_color = 'grey'
            else:
                if isinstance(pin_date, pd.Timestamp):
                    pin_date = pin_date.date()

                pin_color = 'red' if pin_date > datetime.today().date() else 'green'

            html_table = df.loc[[row.name]].to_html(classes="table table-striped table-hover table-condensed table-responsive", index=False)

            popup_content = f"<b>Site ID:</b> {row['Site ID']}<br><b>Postcode:</b> {row['Postcode']}<br><b>Address:</b> {row['Address 1']}<br>{html_table}"

            folium.Marker(
                location=coordinates,
                icon=folium.Icon(color=pin_color),
                popup=folium.Popup(popup_content, max_width=800),
                siteIDsearch=row['Site ID'],  
                postcodesearch=row['Postcode']  
            ).add_to(marker_cluster)

    uk_map.add_child(marker_cluster)

    servicesearch = Search(
        layer=marker_cluster,
        search_label="siteIDsearch",  
        placeholder='Search for a Site ID',
        collapsed=False,
    ).add_to(uk_map)

    postcodesearch = Search(
        layer=marker_cluster,
        search_label="postcodesearch", 
        placeholder='Search for a Postcode',
        collapsed=False,
    ).add_to(uk_map)

    return uk_map

visualize_locations_with_cluster_markers(df).save("map_with_search_and_cluster.html")
print("Map saved as HTML.")

如何获取代码 1 来搜索站点 ID 和邮政编码,这似乎在代码 2 中有效,但需要它在代码 1 中工作。

python-3.x 搜索 folium

评论


答: 暂无答案