python - 如何在地图上插入文本作为图标
问题描述
在地图上插入文本作为图标我已附上错误图像此代码在地图上标记纬度经度但我必须在每个上插入文本。我正在使用 folium 插入符号我应该编辑什么以使文本可见 错误代码图像
import folium
import branca
from folium import plugins
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv(r"C:\Users\VinitJ\Documents\lat_long.csv")
df.head()
df
legend_html ="""
<table width = 100%, style="font-size: 20px;">
<tr><td style="color:blue">Less than equal to 50 <i class="fa fa-map-marker fa-2x"
style="color:blue"></td>
<td style="color:green">between 50 to 100    <i class="fa fa-map-marker fa-2x"
style="color:green"></td>
<td style="color:yellow">between 100 to 200    <i class="fa fa-map-marker fa-2x"
style="color:yellow"></td>
<td style="color:orange">above 200    <i class="fa fa-map-marker fa-2x"
style="color:red"></td>
</table>
"""
folium.Element(legend_html)
m = folium.Map([39.4133, -105.7567], zoom_start=5,width = 5000, height = 2500, top = 20)
m.get_root().html.add_child(folium.Element(legend_html))
#for index, row in df.iterrows():
# folium.Marker([row['lat'], row['lon']],
# #popup=row['City','COUNTRY'],
# icon=folium.Icon(icon='')
# ).add_to(m)
#m.save('map4.html')
#m.get_root().html.add_child(folium.Element(legend_html))
#df.rename(columns={'Peering DB code':'PeeringDBcode','SITE CODE':'SITECODE','SITE ADDRESS':'SITEADDRESS','Site Type':'SiteType','IP PoP Status':'IPPoPStatus','IP SLA':'IPSLA','In Service Date':'InServiceDate'},inplace =True)
def fancy_html(row):
i = row
city = df['city'].iloc[i]
Value = df['value'].iloc[i]
left_col_colour = "#2A799C"
right_col_colour = "#C5DCE7"
html = """<!DOCTYPE html>
<html>
<head>
<h4 style="margin-bottom:0"; width="300px">{}</h4>""".format(city) + """
</head>
<table style="height: 126px; width: 300px;">
<tbody>
<tr>
<td style="background-color: """+ left_col_colour +""";"><span style="color: #ffffff;">Top PLot</span></td>
<td style="width: 200px;background-color: """+ right_col_colour +""";">{}</td>""".format(city) + """
</tr>
<tr>
<td style="background-color: """+ left_col_colour +""";"><span style="color: #ffffff;">Value</span></td>
<td style="width: 200px;background-color: """+ right_col_colour +""";">{}</td>""".format(Value) + """
</tr>
</tbody>
</table>
</html>
"""
return html
#location = df['lat'].mean(), df['lon'].mean()
#m = folium.Map(location=location,zoom_start=15,min_zoom=5)
#for i in range(0,len(df)):
# html = fancy_html(i)
#
# iframe = branca.element.IFrame(html=html,width=400,height=300)
# popup = folium.Popup(iframe,parse_html=True)
#
# folium.Marker([df['Latitude'].iloc[i],df['Longitude'].iloc[i]],
# popup=popup,icon=folium.Icon(color=color, icon='info-sign')).add_to(m)
#
text =df['value']
for i in range(0,len(df)):
num_of_casualties = df['value'].iloc[i]
if num_of_casualties <= 50:
color = 'blue'
elif num_of_casualties <= 100:
color = 'green'
elif num_of_casualties <= 200:
color = 'yellow'
else:
color = 'red'
html = fancy_html(i)
iframe = branca.element.IFrame(html=html,width=400,height=200)
popup = folium.Popup(iframe,parse_html=True)
#folium.map.Marker(
#[df['lat'].iloc[i],df['lon'].iloc[i]],
#icon=DivIcon(
# icon_size=(150,36),
# icon_anchor=(0,0),
# html='<div style="font-size: 24pt">%s</div>' % text,
# )
# .add_to(m)
folium.CircleMarker([df['lat'].iloc[i],df['lon'].iloc[i]],radius=12,color=color,
popup=popup,fill_color=color).add_to(m)
#m.get_root().html.add_child(folium.Element(legend_html))
m.save('map21.html')
此代码在地图上标记纬度经度,但我必须在每个代码上插入文本。我正在使用 folium 插入符号我应该编辑什么以使文本可见
解决方案
folium 有一个DivIcon
你可以使用的类。您可以传递将用于创建标记的任何 html。例如:
div = folium.DivIcon(html=(
'<svg height="100" width="100">'
'<circle cx="50" cy="50" r="40" stroke="yellow" stroke-width="3" fill="none" />'
'<text x="30" y="50" fill="black">9000</text>'
'</svg>'
))
folium.Marker((0, 0), icon=div).add_to(m)
推荐阅读
- neo4j - Neo4J - 使用 Predicate 函数的 Cypher 查询未获取所需的输出
- uninstallation - 我的程序是否负责删除 APPDATA 漫游文件夹?
- julia - Julia:方法无法访问转换方法的可能原因是什么?
- batch-file - 如何在批处理文件的 for 循环之外使用变量?
- layer - 测深:混合层CAPE计算
- sapui5 - `this.oType.formatValue` 不是函数
- javascript - 在 Angular 规则中创建组件
- jpa - JPA 调用存储过程的变化
- javascript - 不要在我的数组中使用空格 [Javascript]
- docker - Docker CentOS systemctl 不允许