简体   繁体   English

如何在 FOLIUM MAP (Python) 中打印 for 循环的索引?

[英]How to print index of for loop in FOLIUM MAP (Python)?

I am writing the following Python code to print numbers beside Folium ICON.我正在编写以下 Python 代码来打印 Folium ICON 旁边的数字。 I need these indices to be dynamic, meaning, I have a for loop, I want index value to be printed in the HTML line of the code.我需要这些索引是动态的,也就是说,我有一个 for 循环,我希望在代码的 HTML 行中打印索引值。

for point in range(0, len(coordinates_st)):
    # showing number
    folium.Marker(location=[72.89, -124.59+2], icon=DivIcon(
        icon_size=(150, 36),
        icon_anchor=(7, 20),
        html='<div style="font-size: 18pt;">r{point}</div>',
    )).add_to(map_st)

I want to print number beside the HOME in the image below (loop indexing from for loop, not fixed 1, 2 as currently written in the code).我想在下图中的 HOME 旁边打印数字(for 循环的循环索引,不是当前代码中编写的固定 1、2)。

    for point in range(0, len(coordinates_st)):
        # showing number
        folium.Marker(location=[72.89, -124.59+2], icon=DivIcon(
            icon_size=(150, 36),
            icon_anchor=(7, 20),
            html='<div style="font-size: 18pt;">1, 2</div>',
        )).add_to(map_st)

当前的folium地图

In this case, the index can be displayed using f-string notation.在这种情况下,可以使用 f 字符串表示法显示索引。 Since the data is not presented, we can use geopandas data to display the usual markers for the cities of the world, with the index next to them.由于未显示数据,我们可以使用 geopandas 数据来显示世界城市的常用标记,并在其旁边显示索引。 characters.人物。

import folium
import geopandas as gpd

geo_df = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))

map_st = folium.Map(location=[50.08528, 14.46403], tiles="OpenStreetMap", zoom_start=4)

for i,row in geo_df.iterrows():
    folium.Marker(location=[row.geometry.y, row.geometry.x+2],
                  icon=folium.DivIcon(
                      icon_size=(150, 36),
                      icon_anchor=(7, 20),
                      html=f'<div style="font-size: 12pt;">{i}</div>',
    )).add_to(map_st)
    folium.Marker(location=[row.geometry.y, row.geometry.x],icon=folium.Icon(color="black", icon="home"),
).add_to(map_st)
map_st

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM