Folium MarkerCluster and Layers merging Clusters

104 Views Asked by At

I'm currently working on a map of supermarkets in the Netherlands and have different Layers for each supermarket as shown in the first picture. However for each layer there is a cluster on the map and these are not merged together as larger clusters. I would like to have the layer controls as in the first picture and the clustering of the second picture. To achieve this I'm using the code from Folium put markers in marker clusters AND in layers based on a value of the user r-beginners. What's the right way to have the best of both worlds?

import pandas as pd
import numpy as np
import folium
from folium.plugins import MarkerCluster

stores = [(-23.5578906,-46.6665546, 'store1','ALDI'),
             (-23.562711,-46.674363, 'store2','LIDL'),
             (-23.5642399,-46.6681833, 'store3','ALDI'),
             (-23.584167,-46.678497, 'store4','LIDL'),
             (-23.5956238,-46.6865377, 'store5','ALDI'),
             (-23.5868682,-46.6773554,'store6','LIDL'), 
             (-23.6011096,-46.6739275, 'store7','ALDI'),
             (-23.6087354,-46.6973713, 'store8','LIDL'),
             (-23.5943515,-46.6846959, 'store9','ALDI')]

df = pd.DataFrame(stores, columns=['LAT','LNG','NAME','CATEGORY'])

mymap = folium.Map(location=[df['LAT'].mean(), df['LNG'].mean()], zoom_start=12)

mCluster_ALDI = MarkerCluster(name="ALDI").add_to(mymap)
mCluster_LIDL = MarkerCluster(name="LIDL").add_to(mymap)

for row in df.itertuples():
    #print(row)
    location = row[1], row[2]
    marker = folium.Marker(location=location)
    if row[4] == 'ALDI':
        mCluster_ALDI.add_child(marker)
    elif row[4] == 'LIDL':
        mCluster_LIDL.add_child(marker)    

folium.LayerControl().add_to(mymap);

mymap

enter image description here enter image description here

0

There are 0 best solutions below