In this post I show you how to plot lines in Folium – the python module for plotting leaflet maps.
Add PolyLine to Map
Like ‘markers’, lines are added to folium map objects with the add_to() method
folium.PolyLine(points).add_to(my_map)
where “points” is a list of tuples containing latitude and longitude information, and “my_map” is a folium map object.
By default this will give an output similar to:
Adjust Line Properties
We can alter the line properties (colour, weight and opacity) with:
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
Plot Lines and Markers
It can also be useful to plot both lines and markers on the same map. This can be easily done by using the Marker.add_to() method, and looping over each of the coordinate pairs in our list:
#add markers for each in points: folium.Marker(each).add_to(my_map) #add lines folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
An Example
Here’s an example involving loading in GPX coordinates from a GPS device. You can also download the code here.
import gpxpy import gpxpy.gpx import folium gpx_file = open('path_to_gpx_file.gpx', 'r') gpx = gpxpy.parse(gpx_file) points = [] for track in gpx.tracks: for segment in track.segments: for point in segment.points: points.append(tuple([point.latitude, point.longitude])) print(points) ave_lat = sum(p[0] for p in points)/len(points) ave_lon = sum(p[1] for p in points)/len(points) # Load map centred on average coordinates my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14) #add a markers for each in points: folium.Marker(each).add_to(my_map) #fadd lines folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map) # Save map my_map.save("./gpx_berlin_withmarker.html")
hey cheers for all these excellent posts on Folium, it seems a great little library 🙂
Please Obiwan you’re my only hope! I’ve been having a very difficult time getting my polylines to show up on my map. I have the below code snippet and if I remark out the Polyline lines the circle markers come up just fine. If I unremark them only the first set of circle markers in the loop show up and the polylines do not show either. I’ve looked at this for so long my eyes are crossing. I’ve tried adding “.add_to(map)” to the end of the each line but that hasn’t helped.
a = geolocator.geocode(alo)
b = geolocator.geocode(zlo)
a1= [aCoord.latitude, aCoord.longitude]
b1= [zCoord.latitude, zCoord.longitude]
fg.add_child(folium.CircleMarker(location=[a.latitude, a.longitude],
fill=’true’,
radius = 6,
popup= ‘Hi’,
fill_color=’blue’,
color = ‘clear’,
fill_opacity=1))
fg.add_child(folium.CircleMarker(location=[b.latitude, b.longitude],
fill=’true’,
radius = 6,
popup= ‘bye’,
fill_color=’red’,
color = ‘clear’,
fill_opacity=1))
fg.add_child(folium.PolyLine(a1, color=”blue”, weight=2.5, opacity=1))
fg.add_child(folium.PolyLine(b1, color=”magenta”, weight=2.5, opacity=1))
Hey,
Is this at all what you are trying to do?
import folium
points_a = [[1,50], [1.2,50.3], [1.23, 50.7]]
points_z = [[1,51], [1.2,51.3], [1.23, 51.7]]
# Load map centred on average coordinates
ave_lat = sum(p[0] for p in points_a)/len(points_a)
ave_lon = sum(p[1] for p in points_a)/len(points_a)
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=9)
#add a markers ‘a’
for each in points_a:
my_map.add_child(folium.CircleMarker(location=each,
fill=’true’,
radius = 6,
popup= ‘Hi’,
fill_color=’blue’,
color = ‘clear’,
fill_opacity=1))
# add markers ‘z’
for each in points_z:
#folium.Marker(each).add_to(my_map)
my_map.add_child(folium.CircleMarker(location=each,
fill=’true’,
radius = 6,
popup= ‘Bye’,
fill_color=’yellow’,
color = ‘clear’,
fill_opacity=1))
# add lines
folium.PolyLine(points_a, color=”green”, weight=2.5, opacity=1).add_to(my_map)
folium.PolyLine(points_z, color=”blue”, weight=2.5, opacity=1).add_to(my_map)
# Save map
my_map.save(“./lines_withmarker.html”)
https://uploads.disquscdn.com/images/c40becfa4f64b4181bbed0a91342259ca8bcc73c160b1377269f4b453677383a.png
Oh man I’m sorry I didn’t get any notification that you responded, thank you so much! 😀
Would you happen to know if there is a built in offset for the markers? I’m mapping out hundreds of routes in a small region and they are all starting to overlap which is making it difficult to click on the markers to get details. I’m thinking a random offset might help but i’m unsure as to how to do that.
No worries :-). I’m not sure of anything off the top of my head, although you might want to look into using marker clusters which group together markers depending on zoom level.
I had a simple go a this here – https://deparkes.co.uk/2016/07/08/uk-university-locations/ . I’m not sure how it would work with the lines between the markers though
Oh man this is awesome! Even if the poly lines don’t end up looking right this is too cool! thanks for opening my eyes to folium awesomesauce!