-1

Problem

I'm mapping coordinates to a map using folium, my coordinates are stored in a list such as this e.g:

[[51.52765, -0.1322611111111111], [53.54326944444444, -2.633125]].

Now when I tell folium to map a coordinate from the list, I have to specify it like this:

folium.Marker(all_coords[1]).add_to(m) #[1] = the first set of coordinates from my list.


My list contains a lot of coordinates and in order to print them all out I have to do this:

folium.Marker(all_coords[1]).add_to(m) #[1] = the first set of coordinates from my list.
folium.Marker(all_coords[2]).add_to(m) #[2] = the second set of coordinates from my list.
folium.Marker(all_coords[3]).add_to(m) #[3] = the third set of coordinates from my list.
folium.Marker(all_coords[4]).add_to(m) #[4] = the fourth set of coordinates from my list.
folium.Marker(all_coords[5]).add_to(m) #[5] = the fifth set of coordinates from my list.

How would I go about telling folium to read all items from my list one by one?


Any help is appreciated thank you :)

name
  • 19
  • 1
  • 5
  • 4
    `for coord in all_coords: folium.Marker(coord).add_to(m)`? – roganjosh Apr 04 '20 at 11:45
  • @matt Could you post that as an answer and close the question? – Nathan Apr 04 '20 at 12:01
  • The suggested duplicate doesn't have an accepted answer and the top-voted answer links to a notebook for code samples. I suggest posting and accepting an answer here instead. – dspencer Apr 04 '20 at 12:18
  • 1
    @dspencer it's literally just a `for` loop, though :/ The OP merely needed to implement what they said in the title – roganjosh Apr 04 '20 at 12:26

1 Answers1

1

Yes , @roganjosh is absolutely right. What you are trying to achieve can be done using the python for loop or while loop.

Of those the for loop is the best since looping variable is automatically defined.
Therefore instead of indexes, you can iterate through the list by:

 for i in all_coords:
    folium.Marker(i).add_to(m)

This is also clearly shown here.

Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34