0

I have a problem. I can't use the MarkerCluster and LayerControl together. I need to add the MarkerCluster to the following code but unfortunately something still does not work. Can anyone tell me how to do it?

import pandas as pd
import folium
from folium.features import CustomIcon
from folium.plugins import MarkerCluster

df1 = pd.read_csv("/home/dominik/projekt/results/pkobp.csv")
df2 = pd.read_csv("/home/dominik/projekt/results/pekao.csv")

logo_PKOBP = 'https://upload.wikimedia.org/wikipedia/commons/8/8d/Logotyp_PKO_BP.svg'
logo_PEKAO = 'https://upload.wikimedia.org/wikipedia/commons/e/ee/Bank_Pekao_SA_Logo_%282017%29.svg'

frames = [df1, df2]

locationlist1 = df1[["Szerokość","Długość"]].values.tolist()
locationlist2 = df2[["Szerokość","Długość"]].values.tolist()

m = folium.Map(location=[52.2138, 20.9795], zoom_start=12, control_scale=True)

pkobp_layer = folium.FeatureGroup(name="PKO BP SA")
marker_cluster = MarkerCluster().add_to(pkobp_layer)
for point in range(len(locationlist1)):
    PKOBP = folium.Marker(locationlist1[point], icon=CustomIcon(logo_PKOBP, icon_size=(40, 40))).add_to(marker_cluster)

pekao_layer = folium.FeatureGroup(name="PEKAO SA")
marker_cluster = MarkerCluster().add_to(pekao_layer)
for point in range(len(locationlist2)):
    PEKAOSA = folium.Marker(locationlist2[point], icon=CustomIcon(logo_PEKAO, icon_size=(150, 22))).add_to(marker_cluster)

m.add_child(pkobp_layer)
m.add_child(pekao_layer)

m.add_child(folium.map.LayerControl())

m.save('/home/dominik/mapa1.html')
BigD
  • 77
  • 10

1 Answers1

0

You did a mistake similar to mine.

I think this answer should help you:

https://gis.stackexchange.com/questions/443392/python-folium-markercluster-doesnt-work-as-expected-for-the-add-child-element

Instead of adding the markercluster to your current layer, you should have done it for the map. Next, your layer will be appended as the child to the defined markercluster.

In your code:

   m = folium.Map(location=[52.2138, 20.9795], zoom_start=12, control_scale=True)

  df1 = pd.read_csv("/home/dominik/projekt/results/pkobp.csv")

  pkobp_layer = folium.FeatureGroup(name="PKO BP SA")
  marker_cluster = MarkerCluster().add_to(m)

  for point in range(len(locationlist1)):
  PKOBP = folium.Marker(locationlist1[point], icon=CustomIcon(logo_PKOBP, 
  icon_size=(40, 40)))

  marker_cluster.add_child(PKOBP)

And the same for the other one. In turn, the clustering will be applied to all your layers simultaneously.

Geographos
  • 827
  • 2
  • 23
  • 57