首页 > 解决方案 > 如何在地图上插入文本作为图标

问题描述

在地图上插入文本作为图标我已附上错误图像此代码在地图上标记纬度经度但我必须在每个上插入文本。我正在使用 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&nbsp;&nbsp;&nbsp;<i class="fa fa-map-marker fa-2x"
                  style="color:blue"></td>

     <td style="color:green">between 50 to 100&nbsp&nbsp&nbsp&nbsp<i class="fa fa-map-marker fa-2x"
                  style="color:green"></td>
    <td style="color:yellow">between 100 to 200&nbsp&nbsp&nbsp&nbsp<i class="fa fa-map-marker fa-2x"
                  style="color:yellow"></td>
     <td style="color:orange">above 200&nbsp&nbsp&nbsp&nbsp<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 插入符号我应该编辑什么以使文本可见

标签: pythonpython-3.xfolium

解决方案


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)

推荐阅读