-1

I have a dataset of location and some random values assigned to each location like following dataset picture.

I have fetched the geographic location of each area, saved it in a json file, and then showed it accordingly on the map using markers. But what I want is to show markers based on the total values. That means showing as many markers as the values assigned to each location. How can I do that? here is my code:

data = pd.read_json("Address.json")
lat = list(data["Latitude"])
lon = list(data["Longitude"])
total = list(data["Total"])

map = folium.Map(location =[23.6850,90.3563])

f_group = folium.FeatureGroup(name="Map")

for lt,ln,total in zip(lat,lon,total):
    f_group.add_child(folium.CircleMarker(location = [lt,ln], popup="Affected: "+str(total),radius=5, fill_color='red',color='grey',fill_opacity=1))

map.add_child(f_group) 
map.save("Map1.html")
  • Maybe [markercluster](https://medium.com/@bobhaffner/folium-markerclusters-and-fastmarkerclusters-1e03b01cb7b1)? Comment back if so and I can help. – zerecees Apr 23 '20 at 04:52
  • I am not sure if markercluster will work. I think the method of the post that you provided only works when there are multiple rows or values assigned to a same location. But in my case, the total values assigned to each location are presented as a single integer. If you want to try though, please do. – Monjur Bin Shams Apr 23 '20 at 05:09
  • Ah ok, I think I'm getting closer to what you want. To clarify, let's say your total is 5 at lat/long 123/123. You want 5 markers for the place at lat/long 123/123? – zerecees Apr 23 '20 at 05:13
  • That is exactly what I want, you understood it. – Monjur Bin Shams Apr 23 '20 at 05:22
  • Gotcha, hang on, I'm writing my answer. – zerecees Apr 23 '20 at 05:27

1 Answers1

0

If you want to use just markers, try:

for lt,ln,total in zip(lat,lon,total):
  for i in range(total):
    f_group.add_child(folium.CircleMarker(location = [lt,ln], popup="Affected: "+str(total),radius=5, fill_color='red',color='grey',fill_opacity=1))

But watch out for how many add_child's you do. That may slow down the map quite a bit depending on how you plan to interact with it.

zerecees
  • 697
  • 4
  • 13
  • ```TypeError Traceback (most recent call last) in 9 10 for lt,ln,total in zip(lat,lon,total): ---> 11 for i in range(len(total)): 12 f_group.add_child(folium.CircleMarker(location = [lt,ln], popup="Affected: "+str(total),radius=5, fill_color='red',color='grey',fill_opacity=1)) 13 TypeError: object of type 'int' has no len()``` – Monjur Bin Shams Apr 23 '20 at 05:55
  • Edited, use `range(total)` instead of `(range(len(total))` You can't do `len()` on an int because there isn't a length, but you can do range. – zerecees Apr 23 '20 at 05:59
  • The code is working fine, but I have doubts if it is adding the markers as per the total values because the map is showing the markers the same as before. Perhaps the markers for the same location are being added on top of one another. I am gonna go through the codes and dataset again to see if there is any sort of glitch – Monjur Bin Shams Apr 23 '20 at 06:09
  • folium should be stacking the markers, as seen [here](https://gis.stackexchange.com/questions/70649/how-to-control-the-order-of-appearance-of-markers-using-leaflet). I would strongly recommend marker clusters at this point. But, always happy to help if you have any questions. – zerecees Apr 23 '20 at 06:14
  • Alright, thanks a lot. I am gonna look into it to shape my code conveniently. – Monjur Bin Shams Apr 23 '20 at 06:19
  • I have solved it mate, by adding an extra loop. But you were right, it severely slows down the code as well as the map haha. So I had to cut it out. But learnt a lot, thanks for the suggestions. If you want, I can show you my modified code. If not, then its fine – Monjur Bin Shams Apr 23 '20 at 07:13
  • Hey man! Glad you figured it out/learned some new things. Up to you on finished code. I'll help with anything I can. Let me know! – zerecees Apr 23 '20 at 16:27