没有出现 Folium 弹出窗口 python

Folium popup does not appear python

我有一个包含多字符串几何的地理数据框。我想绘制一个交互式地图,当我点击每一行时,名称和 ID 就会出现!我可以看到鼠标形式会改变,但什么也不会出现。我也尝试将名称定义为标签,但它没有用!

Ex_centreLine_map= folium.Map(location = [43.7180, -79.3762], zoom_start=12)

folium.Choropleth(
    centreline_gdf[centreline_gdf.geometry.length> 0.00001],
    line_weight=3,
    line_color='blue',
    popup=centreline_gdf[['lf_name', 'lfn_id']]).add_to(Ex_centreLine_map)
Ex_centreLine_map

  • 你没有提供任何样本数据,所以我用一些道路数据做了一个MWE
  • 你可以做原生的 folium 但是使用起来更简单 geopandas explore()
  • 对于原生 folium 你可以使用 folium.GeoJson()
import geopandas as gpd
import pandas as pd
import shapely.wkt
import io

df = pd.read_csv(io.StringIO("""ref;lanes;highway;maxspeed;length;name;geometry
A3015;2;primary;40 mph;40.68;Rydon Lane;MULTILINESTRING ((-3.4851169 50.70864409999999, -3.4849879 50.7090007), (-3.4857269 50.70693379999999, -3.4853034 50.7081574), (-3.488620899999999 50.70365289999999, -3.4857269 50.70693379999999), (-3.4853034 50.7081574, -3.4851434 50.70856839999999), (-3.4851434 50.70856839999999, -3.4851169 50.70864409999999))
A379;3;primary;50 mph;177.963;Rydon Lane;MULTILINESTRING ((-3.4763853 50.70886769999999, -3.4786112 50.70811229999999), (-3.4746017 50.70944449999999, -3.4763853 50.70886769999999), (-3.470350900000001 50.71041779999999, -3.471219399999999 50.71028909999998), (-3.465049699999999 50.712158, -3.470350900000001 50.71041779999999), (-3.481215600000001 50.70762499999999, -3.4813909 50.70760109999999), (-3.4934747 50.70059599999998, -3.4930204 50.7007898), (-3.4930204 50.7007898, -3.4930048 50.7008015), (-3.4930048 50.7008015, -3.4919513 50.70168349999999), (-3.4919513 50.70168349999999, -3.49137 50.70213669999998), (-3.49137 50.70213669999998, -3.4911565 50.7023015), (-3.4911565 50.7023015, -3.4909108 50.70246919999999), (-3.4909108 50.70246919999999, -3.4902349 50.70291189999999), (-3.4902349 50.70291189999999, -3.4897693 50.70314579999999), (-3.4805021 50.7077218, -3.4806265 50.70770150000001), (-3.488620899999999 50.70365289999999, -3.4888806 50.70353719999999), (-3.4897693 50.70314579999999, -3.489176800000001 50.70340539999999), (-3.489176800000001 50.70340539999999, -3.4888806 50.70353719999999), (-3.4865751 50.70487679999999, -3.4882604 50.70375799999999), (-3.479841700000001 50.70784459999999, -3.4805021 50.7077218), (-3.4882604 50.70375799999999, -3.488620899999999 50.70365289999999), (-3.4806265 50.70770150000001, -3.481215600000001 50.70762499999999), (-3.4717096 50.71021009999998, -3.4746017 50.70944449999999), (-3.4786112 50.70811229999999, -3.479841700000001 50.70784459999999), (-3.471219399999999 50.71028909999998, -3.4717096 50.71021009999998))"""),
           sep=";")

gdf = gpd.GeoDataFrame(df, geometry=df["geometry"].apply(shapely.wkt.loads), crs="epsg:4326")

gdf.explore(style_kwds={"weight":10})

原生叶

import folium

m = folium.Map(
    location=[sum(gdf.total_bounds[[1, 3]]) / 2, sum(gdf.total_bounds[[0, 2]]) / 2],
    zoom_start=12,
)

def style_fn(x):
    return {"color":"blue", "weight":3}

folium.GeoJson(
    gdf,
    style_function=style_fn,
    popup=folium.GeoJsonPopup(gdf.drop(columns=["geometry"]).columns.tolist()),
    tooltip=folium.GeoJsonTooltip(["ref"]),
).add_to(m)

m