[No audio]
Hi again, and welcome to this new
lecture. In this lecture, you're going
to learn how to add points to the map
that we built previously, which is this
one here. So all we have here is a base
map. So the background map, which is a
layer, actually some layer coming from
OpenStreetMap service.
[No audio]
So OpenStreetMaps, they give you a layer,
actually folium will serve you this
layer on the background through
JavaScript. But you can also add other
layers as well, other base layers, and
also points, which you can add on top of
the base layer. So let me go to back to
the code. This is what we wrote in the
previous lecture. Actually, we wrote it
in here, in the interactive shell. But I
just copied and pasted this in a file so
that we have it, and that we can add more
code now on top of this. So again, as I
told you can do help folium.Map
and you see you can add other things in
here, such as here you have the Parameters.
So again, add the tiles, the default is OpenStreetMaps,
but if you specify another tile, such as
[No audio]
Mapbox Bright and that has to be
in quotes. As you can see in here,
[No audio]
in the tiles parameter, this expects a
string object, location for instance, it
expects a tuple or a list, and that's
what we pass, we pass a list of
coordinates, though it would be an
integer, an integer height. As long you
have other options as well, which you can try
by yourself, just want to try this one for now.
[No audio]
Let me open a new Python Shell
[No audio]
python3 map1, make sure you have
saved your script, execute.
[No audio]
Then you can go back to the folder and open map.
Let me go to Firefox, Ctrl R, or Command R to reload.
And you'll see that the background has changed.
So we have a new base map now.
[No audio]
Okay, let's go back to the script now.
And what I'm going to do is I'm going to
add some point markers on top of the
base map, and so how do you do that? Well,
if you do dir folium.
[No audio]
here you can see that you have some objects.
And the one we want is a marker, or a
CircleMarker. So I'll try them both. But I
want to explain you the logic on how you
add objects to the Map object.
So we import folium, we create the Map object, and
then between the Map object and the Save
methods of the Map object, you can add
elements, so objects to that map, and
what I want to add in this case is a marker. So
I can do map.add_child.
So we use the add_child method
to add children. So these are objects
that I'm talking about are referred to
as children when you add them to the
map. So map.add_child, and then you
want to point to the folium.Marker.
Now this Marker method, this expects some
arguments itself, you can go help folium.Marker.
[No audio]
So create a simple stock
Leaflet marker on the map. Leaflet is a
JavaScript library that helps you to
visualize this, that is the engine that
creates those maps on the browser.
So Python creates the JavaScript code using
the Leaflet library and. So markers
allow you to add popups. So when you
click a marker on the map, it allows you
to show information, and so parameters
you need to pass in location parameter
to the marker in order to visualize it.
So location equals to, so it's a
tuple or list, and let's say 38.2
and -99.1 and you can also pass.
So after the list a popup
[No audio]
parameter, which should be like, Hi
[Author typing]
I am a Marker. I also need to pass
the icon parameter, which actually is
another object that comes from folium.Icon
[No audio]
that which expects for me
to pass a color there, like, let's say
green and then basically, that's it.
Make sure your brackets are correct. So
have three brackets here, three closing
brackets, and one, and two, and three,
opening around brackets. Okay.
[No audio]
So the script, go to the other terminal and
execute, go to Firefox, reload, and here is a
marker. If I click, I get the popup
message in a nice window here.
[No audio]
So that's how you add markers to your map.
However, I would suggest you follow a
slightly different approach to add
a children to your map. Now, what else
suggests is to actually create a feature group
folium.FeatureGroup
[No audio]
and you can give it a name,
[No audio]
like My Map, and then what happens is that
in this feature group, so the marker is a
feature, and with feature groups, you can
add a multiple features to the feature
groups, so you can add the marker, you can add the
like polygons, etc. And so instead of
saying map, you do the feature group
object, so this variable that I created
in here, I'm passing here, and then
after that, you do map.add_child,
and you pass the feature group in
there. So this is more. So this is done
to keep your code more organized, and it
also helps you later when you want to
add a control layer, a layer
control feature in your map to allow you
to switch other layers on and off. So we'll
look at this later, and save this script, and let me try.
[No audio]
Reload the page.
[No audio]
So we don't have any change,
things we got the same,
which means a feature
group is working fine. But as I said,
it's good to keep the code organized.
And it also helps you later when you add
a layer control feature. That's what I
wanted to teach you for now. And I'll
talk to you later.