You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Folium设置GeoJson多点颜色无效:始终显示蓝色问题求助

Folium多点(MultiPoint)颜色不生效问题解决

你的代码里style_function的color参数只对线、面类几何要素生效,点要素(包括MultiPoint)的样式默认不受该参数控制,所以会显示默认蓝色标记。以下是几种可行的解决方法:

方法1:使用point_style参数设置点样式

直接给GeoJson添加point_style参数,指定点的颜色、填充色等属性:

multipoints = folium.GeoJson(
    device_geometry.to_json(),
    point_style={'color': 'red', 'fillColor': 'red', 'radius': 5}
)
d = folium.FeatureGroup(name='device')
d.add_child(multipoints)
m.add_child(d)

point_style的参数对应Leaflet CircleMarker的配置项,常用的有:

  • radius:点的半径
  • color:点的边框颜色
  • fillColor:点的填充颜色
  • fillOpacity:填充透明度(0-1)

方法2:自定义Marker函数(更灵活)

通过marker_function自定义每个点的Marker样式,适合需要更复杂样式的场景:

def custom_marker(feature):
    # GeoJSON坐标是[经度, 纬度],Folium需要[纬度, 经度],所以反转顺序
    coords = feature['geometry']['coordinates']
    return folium.CircleMarker(
        location=[coords[1], coords[0]],
        radius=6,
        color='red',
        fill=True,
        fillColor='red',
        fillOpacity=0.8
    )

multipoints = folium.GeoJson(
    device_geometry.to_json(),
    marker_function=custom_marker
)
d = folium.FeatureGroup(name='device')
d.add_child(multipoints)
m.add_child(d)

额外检查项

  • 确认device_geometry的几何类型是MultiPoint,可以通过print(device_geometry.geometry.type)验证
  • 确保地图对象m已正确初始化(比如m = folium.Map(location=[30, 120], zoom_start=10))

内容的提问来源于stack exchange,提问作者chun-hsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:55:00