【问题标题】:Is it possible to plot markers based on which LayerControl is activated?是否可以根据激活的 LayerControl 绘制标记?
【发布时间】:2021-11-22 17:31:01
【问题描述】:

我在我的 Folium 地图上设置了两个切片图层。像这样的:

folium_map = folium.Map(location=[50, 50], control_scale=True, zoom_start=2, max_bounds=True, prefer_canvas=False)
folium.TileLayer('openstreetmap').add_to(folium_map)
folium.TileLayer('google maps').add_to(folium_map)

如果选择了 Google 地图,我希望仅显示一个文本标记,如果选择了 OpenStreet 地图,我希望该标记具有文本和图像。

是否可以为不同的瓦片层绘制一组独特的标记?

================================================ ===

我接受了双地图建议,效果很好。我现在正在尝试将子组添加到功能组。但是,对偶映射似乎不喜欢子组。主地图(左侧)有效。次要地图(右侧)反转子组选项,选择后,弹出窗口出现在主屏幕上。有没有办法来解决这个问题?这是我的测试代码:

import folium
from folium import plugins
import pandas as pd

###############################################################################
# File paths
###############################################################################
main_dir = 'E:\Test\\'
output_dir = main_dir + 'Output\\'

###############################################################################
# Create test file
###############################################################################
# List1
Name = ['Tom', 'Mary', 'Juannita', 'Ivan', 'Peter', 'Lisa', 'Juan', 'Boris']
Lat = [ 51.507351, 38.90719,  19.432608,  55.755568, 51.47333, 38.890067,  19.521744,  55.650486]
Lng = [ -0.127758, -77.036871, -99.133208,  37.614361,  -0.134092, -76.984608, -99.219789,  37.732448]
Location = ['London', 'Washington', 'Mexico',  'Moscow', 'London', 'Washington', 'Mexico', 'Moscow']
Color = ['red', 'red', 'blue', 'red', 'blue', 'blue', 'blue', 'red']
Ownership = ['Partner', 'Partner', 'Own', 'Partner', 'Own', 'Own', 'Own', 'Partner']
Type = ['ABC', 'Acme', 'Acme', 'Acme', 'ABC', 'ABC', 'Acme', 'Acme']


  
# Get the list of tuples from two lists and merge them by using zip().
list_of_tuples = list(zip(Name, Lat, Lng, Location, Color, Ownership, Type))
   
# Converting lists of tuples into Pandas Dataframe.
df1 = pd.DataFrame(list_of_tuples,
                  columns = ['Name', 'Latitude', 'Longitude', 'Location', 'Color', 'Ownership','Type'])

###############################################################################
# Folium Map Setup
###############################################################################
folium_map = folium.plugins.DualMap(location=[50, 50],
                                    control_scale=True,
                                    zoom_start=9,
                                    tiles=None,
                                    max_bounds=True, 
                                    prefer_canvas=False)

folium_map.fit_bounds([[180, 89], [-180, -89]])

folium.TileLayer('openstreetmap').add_to(folium_map.m1)

folium.raster_layers.TileLayer(
    tiles="http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
    attr="google",
    name="google maps",
    max_zoom=20,
    subdomains=["mt0", "mt1", "mt2", "mt3"],
    overlay=False,
    control=True,
    ).add_to(folium_map.m2)

minimap = plugins.MiniMap(toggle_display=True)
folium_map.add_child(minimap)

plugins.Fullscreen(
    position='topright',
    title='Expand me',
    title_cancel='Exit me',
    force_separate_button=True
    ).add_to(folium_map)

###############################################################################
# Layer Control:
##############################################################################@
mcg = folium.plugins.MarkerCluster(name="Locations with Images", control=False) # create marker clusters
folium_map.add_child(mcg)

###############################################################################
# ABC Stores
###############################################################################
fg1 = folium.FeatureGroup(name='ABC Stores')
folium_map.add_child(fg1)

g1 = plugins.FeatureGroupSubGroup(fg1, '\tOwn - ABC', show=False)
folium_map.add_child(g1)

g2 = plugins.FeatureGroupSubGroup(fg1, '\tPartnership - ABC', show=False)
folium_map.add_child(g2)

for index, row in df1.iterrows():
    if row['Type'] == 'ABC':
        kw = {"color": row['Color'], "icon": "shopping-cart"}
        icon_symbol = folium.Icon(**kw)
        popup = row['Name']
        if row['Ownership'] == 'Own':
            folium.Marker(
                location = [row['Latitude'], row['Longitude']],
                draggable = False,
                clustered_marker=True,
                tooltip = '<strong>' + str(row['Location'] + '</strong>'),
                popup = folium.Popup(popup, max_width=350),
                icon=icon_symbol
                ).add_to(g1)
        else:
            folium.Marker(
                location = [row['Latitude'], row['Longitude']],
                draggable = False,
                clustered_marker=True,
                tooltip = '<strong>' + str(row['Location'] + '</strong>'),
                popup = folium.Popup(popup, max_width=350),
                icon=icon_symbol
                ).add_to(g2)

###############################################################################
# Acme Fitness
###############################################################################
fg2 = folium.FeatureGroup(name='Acme Fitness')
folium_map.add_child(fg2)

g1 = plugins.FeatureGroupSubGroup(fg2, '\tOwn - ACME', show=False)
folium_map.add_child(g1)

g2 = plugins.FeatureGroupSubGroup(fg2, '\tPartnership - ACME', show=False)
folium_map.add_child(g2)

for index, row in df1.iterrows():
    if row['Type'] == 'Acme':
        kw = {"prefix": "fa", "color": row['Color'], "icon": "camera"}
        icon_symbol = folium.Icon(**kw)
        popup = row['Name']
        if row['Ownership'] == 'Own':
            folium.Marker(
                location = [row['Latitude'], row['Longitude']],
                draggable = False,
                clustered_marker=True,
                tooltip = '<strong>' + str(row['Location'] + '</strong>'),
                popup = folium.Popup(popup, max_width=350),
                icon=icon_symbol
                ).add_to(g1)
        else:
            folium.Marker(
                location = [row['Latitude'], row['Longitude']],
                draggable = False,
                clustered_marker=True,
                tooltip = '<strong>' + str(row['Location'] + '</strong>'),
                popup = folium.Popup(popup, max_width=350),
                icon=icon_symbol
                ).add_to(g2)

folium.LayerControl().add_to(folium_map)

folium_map.save(output_dir + 'Test.html') 

【问题讨论】:

    标签: python folium


    【解决方案1】:

    我花了很多时间研究这个,但似乎不可能在不同的图层上绘制独特的标记。似乎两张并排的地图会做我想要的。请参阅this page 了解更多信息。请参阅this page 了解如何制作 Google 地图的平铺层。

    更新 代码和图像已更新,以反映问题已被编辑并添加了新问题的事实。使用 subgroup 功能对标记进行分组的关键是建立父子关系。在这种情况下,由于是多图的例子,所以创建了如下的父子关系来实现。

    folium_map.m1 => fg_A(FueatureGroup) => g_1(SubGroup) => folium.Maker(g_1)

    import folium
    import folium.plugins
    
    folium_map = folium.plugins.DualMap(location=[52.5, 5.15],
                                        control_scale=True,
                                        zoom_start=9,
                                        tiles=None,
                                        max_bounds=True, 
                                        prefer_canvas=False)
    
    folium.TileLayer('openstreetmap').add_to(folium_map.m1)
    folium.raster_layers.TileLayer(
        tiles="http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
        attr="google",
        name="google maps",
        max_zoom=20,
        subdomains=["mt0", "mt1", "mt2", "mt3"],
        overlay=False,
        control=True,
    ).add_to(folium_map.m2)
    
    #fg_both = folium.FeatureGroup(name='markers_both').add_to(folium_map)
    fg_A = folium.FeatureGroup(name='markers_1').add_to(folium_map.m1)
    g1 = folium.plugins.FeatureGroupSubGroup(fg_A, 'g1')
    folium_map.m1.add_child(g1)
    
    fg_B = folium.FeatureGroup(name='markers_2').add_to(folium_map.m2)
    g2 = folium.plugins.FeatureGroupSubGroup(fg_B, 'g2')
    folium_map.m2.add_child(g2)
    
    #icon_red = folium.Icon(color='red')
    #folium.Marker((52.6, 5.0), tooltip='both', icon=icon_red).add_to(fg_both)
    g1.add_child(folium.Marker((52.4, 5.0), tooltip="1"))
    g1.add_child(folium.Marker((52.0, 5.4), tooltip="2"))
    g2.add_child(folium.Marker((52.4, 5.6), tooltip="3"))
    g2.add_child(folium.Marker((52.0, 5.8), tooltip="4"))
    
    folium.LayerControl().add_to(folium_map)
    
    folium_map
    

    【讨论】:

    • 希望我的回答能帮助您得到您想要的答案。如果您觉得我的回答有用,请点击对勾接受回答。
    • 感谢您的代码。它确实有帮助,但似乎双地图和 FeatureGroupSubGroupd 不能很好地配合使用。 The left hand map works but the right hand map displays the subgroups in reverse order and when selected, the subgroups appear on the left had side map and not the right hand side.
    • 评论行为与我的环境不同,请查看我提取的Colab代码。
    • 当我把它带到下一个 stahe 并添加子组时,代码工作得非常疯狂,标记在两个地图上都不起作用。
    • 不知道你加了什么群。您不应该编辑问题以便我们看到您添加的内容吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多