Folium: Combining `GeoJson` markers in a `MarkerCluster` shows unclustered markers

Created on 27 Sep 2019  路  7Comments  路  Source: python-visualization/folium

Please add a code sample or a nbviewer link, copy-pastable if possible

with open('examples/data/search_bars_rome.json') as f:
    data = json.load(f)
data_url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/search_bars_rome.json'

m = folium.Map((41.9, 12.5), tiles='cartodbpositron')
marker_cluster = folium.plugins.MarkerCluster(name='mc').add_to(m)
folium.GeoJson(data_url, embed=False).add_to(marker_cluster)
folium.GeoJson(data_url, embed=False, name='geojson').add_to(m)
folium.LayerControl(collapsed=False).add_to(m)
m.save('_map.html')
m

Problem description

I expected to see the markers in a clustered fashion

image

I can't tell from #1190, #1129 and #1188 if this is fixed in version 0.10.0, and if it is, what's the right syntax?

Expected Output

Clustered markers (This was taken from another example)

image

Output of folium.__version__

'0.10.0'

bug

Most helpful comment

The fix has been merged. It will be in the next release, v0.12.0, release date yet unknown. If you want it earlier you can install folium from the main branch:

pip install git+https://github.com/python-visualization/folium.git@master

All 7 comments

Hi @diegoquintanav, I think that #1190 fixed the problem you mention, and it was done after the release of version 10. Therefore, would be better install folium directly from github give it a try again.

Confirmed! It works with folium installed directly from github.
I had time to run your script and here is a screenshot of the html file.

Screenshot_2019-10-24 Screenshot

I am having the same issues I'm afraid. It doesn't render the MarkerCluster at all.

image

image

image

image

Thanks for your help!

It seems #1190 (merged July 2019) and #1289 (merged April 2020) are at odds with each other. We have to resolve the conflicting requirements. Some ideas:

  • make the ajax call synchronous and add to parent when it finished
  • move the embedded data to the GeoJson() call and accept that not al functionality will work when not embedding data.

I think I've found a way round it. If you make the GeoJson a subgroup of the MarkerCluster it seems to work. Although weirdly you need to untick and tick GeoJson layer (Housing) for it show anything.

with open('examples/data/search_bars_rome.json') as f:
    data = json.load(f)
data_url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/search_bars_rome.json'

m = folium.Map((41.9, 12.5), tiles='cartodbpositron')
marker_cluster = folium.plugins.MarkerCluster(name='Developments')
mc1= folium.plugins.FeatureGroupSubGroup(marker_cluster, 'Housing')
m.add_child(marker_cluster)
m.add_child(Housing)
mc1.add_child(folium.GeoJson(data_url, embed=False))
folium.LayerControl(collapsed=False).add_to(m)
m

image

My next query is, how can I show labels, either tooltips or popups? Substituting mc1 with the code below doesn't seem to work.

mc1.add_child(folium.GeoJson(data_url, tooltip=folium.GeoJsonTooltip(fields=['addr:street']), embed=False))

It seems combining GeoJsonTooltip and MarkerCluster plugin is not supported behavior. Note that while everything in the base folium library should work well together we can't guarantee that for the plugins.

I don't think using the FeatureGroupSubGroup plugin is a real solution here. I'll make a PR where we do the ajax call asynchronously.

The fix has been merged. It will be in the next release, v0.12.0, release date yet unknown. If you want it earlier you can install folium from the main branch:

pip install git+https://github.com/python-visualization/folium.git@master
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Demetrio92 picture Demetrio92  路  18Comments

FlorianHoevelmann picture FlorianHoevelmann  路  14Comments

ispmarin picture ispmarin  路  17Comments

xhx509 picture xhx509  路  15Comments

achourasia picture achourasia  路  19Comments