16

I have tried Python folium library with impressive results, but there is one feature I am missing, or in any case I can't find: I want to print a multiline in a new layer over the map.

If I check de documentation, I can only find how to add markers and poligon markers. But about printing in a new layer, I can only find examples like this one.

I need something much simplier than that. I guess I could insert a GeoJSON with the multiline info in a similar way, but I haven't been able to even find which format should that GeoJSON have.

Any idea for getting my multiline?

PD: If you don't know how to achieve this using Python/Folium, I will be happy to hear what should I add to the Javascript output to get the multiline using Leaflet (that's what Folium library is using).

jarmokivekas
  • 187
  • 8
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

3 Answers3

14

Some of the functions in the earlier example are now deprecated; apparently, the preferred method is now something like:

import folium

# Coordinates are 10 points on the great circle from Boston to
# San Francisco.
# Reference: http://williams.best.vwh.net/avform.htm#Intermediate
coordinates = [
    [42.3581, -71.0636],
    [42.82995815, -74.78991444],
    [43.17929819, -78.56603306],
    [43.40320216, -82.37774519],
    [43.49975489, -86.20965845],
    [41.4338549, -108.74485069],
    [40.67471747, -112.29609954],
    [39.8093434, -115.76190821],
    [38.84352776, -119.13665678],
    [37.7833, -122.4167]]

# Create the map and add the line
m = folium.Map(location=[41.9, -97.3], zoom_start=4)
my_PolyLine=folium.PolyLine(locations=coordinates,weight=5)
m.add_child(my_PolyLine)
# m.save('line_example_newer.html')
Phani Rithvij
  • 4,030
  • 3
  • 25
  • 60
Bill Bradley
  • 456
  • 4
  • 16
  • As of June 2016, the PolyLine method appears to be deprecated, and the .line() method used by Roman Rdgz works. Use his example. – emunsing Jun 10 '16 at 23:13
  • 4
    @emunsing Quite the opposite no ? Running the example, I get `line` is deprecated. – Overdrivr Oct 11 '16 at 16:01
8

I finally found a way implemented in Folium in January 2014 and not documented. Its the line method.

Here appears an example provided by the author of this addon.

Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
3

Neither of the above worked for me for adding lines as a new layer to a folium.Map object (using folium 0.11). What works for me is using folium.FeatureGroup:

coords = [[[42.3554025, -71.0728116], [42.3554142, -71.0728438]],
 [[42.3554142, -71.0728438], [42.3554296, -71.0728738]]]
test_map = folium.Map([42.3554025, -71.0728116], tiles='Cartodb Positron', zoom_start=15)
fg = folium.FeatureGroup("Lines")
folium.PolyLine(coords).add_to(fg)
f.add_to(test_map)
folium.LayerControl(position='bottomright').add_to(test_map)
test_map

This prints a map that has a "Lines" layer which, when toggled, will show the lines plotted at the coordinates above.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ben
  • 2,308
  • 2
  • 18
  • 25
  • 2
    upvote from my side : D thanks! Any idea of how I could turn the connected dots into a different color? (eg: keep the lines connecting the dots blue, but make the dots themselves red, for example) – M.Ionut Apr 18 '22 at 21:10
  • 1
    I think this might be a Folium/Leaflet parameter. Check [Folium docs](https://python-visualization.github.io/folium/modules.html#folium.vector_layers.PolyLine), has a set of kwargs that inherit from [Leaflet](https://leafletjs.com/SlavaUkraini/reference-1.6.0.html#polyline). I haven't tried, but it seems like you could mess with those parameters for color. – Ben Apr 22 '22 at 21:18