在地图上标记出重复的点,并显示它们的信息
import pandas as pd
import folium
from folium.plugins import MarkerCluster, FeatureGroupSubGroup
from folium.vector_layers import CircleMarker
# 加载数据
data = pd.read_excel(r'C:\Users\win10\Desktop\spots.xlsx')
# 获取需要的数据
heat_data = [[row['latitude'], row['longitude']] for index, row in data.iterrows()]
# 绘制地图
m = folium.Map(location=[30, 120],
zoom_start=11,
tiles='https://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7',
attr='高德-常规图')
# 使用 MarkerCluster 添加标记
marker_cluster = MarkerCluster().add_to(m)
# 创建FeatureGroupSubGroup对象
fg = FeatureGroupSubGroup(name='Name', group=marker_cluster).add_to(m)
# 将经纬度坐标和对应名称存储到字典中
location_dict = {}
for index, row in data.iterrows():
location = (row['latitude'], row['longitude'])
name = row['name']
if location in location_dict:
location_dict[location]['count'] += 1
location_dict[location]['names'].append(str(name))
else:
location_dict[location] = {'count': 1, 'names': [str(name)]}
# 遍历字典,在地图上添加标记和围圈
for location, info in location_dict.items():
count = info['count']
names = info['names']
popup_text = f"名称: {', '.join(names)}
数量: {count}"
popup = folium.Popup(popup_text, max_width=500)
marker = folium.Marker(location=location, popup=popup)
fg.add_child(marker)
if len(names) > 1:
circle_marker = CircleMarker(location=location,
radius=50,
color='red',
fill_color='blue',
fill_opacity=0.2,
popup=f"名称: {', '.join(names)}
数量: {count}")
fg.add_child(circle_marker)
# 添加图层控制器
folium.LayerControl().add_to(m)
# 保存地图
m.save(r'C:\Users\win10\Desktop\heatmap.html')