Hello Leaflet… For realz..¶
Returning home to the HTML/CSS/JS analogy¶
Recall from last week’s lab (before things went downhill) and the pre-lab reading that a webpage is like a house:
- HTML is the scaffolding/foundation of the house
- CSS is the paint, carpets, etc. that makes the house look nice
- JavaScript is the appliances that adds function to the house
Today we will be focusing on the appliances in more detail.
Why start with HTML and CSS first?¶
In order to do any JavaScript coding, you need to make sure your content has a place to show up! Additionally, with Leaflet, we actually need to bring in Leaflet’s CSS and JavaScript files into our HTML house. Recall that last week, the map did not show up because I forgot to include Leaflet!
Also, recall that we can bring in Leaflet by externally linking just like we did with our CSS (styles/style.css
)!
Prepping our HTML¶
Let’s keep all our content in the .main
class, but move all our portfolio information into a new div
element with an id
of contents
, which looks like this:
<div class="main">
<div id="contents">
<!-- hint: the majority of lab 1 assignment can go here --> <!-- (1)! -->
</div>
</div>
- You should be able to paste the
main
contents of your first lab assignment here.
Compatibility with Lab assignment #1
If you are using your week1
lab assignment, you can copy and paste anything with the div
elements inside the hint! Of course, this will not work if you have different div
or changed the css
too much! Adapt as needed!
In our index.html
header tag right before </header>
, we will need to add the Leaflet.js library. We add it right before the end to make sure they are the last items to load before the body starts loading.
Linking Leaflet.js files¶
<!-- Leaflet's css-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<!-- Leaflet's JavaScript-->
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
Adding the_map
container¶
In our body, our map needs a PLACE to go, so let’s add a <div>
for our map.
divs
vs. spans
, what’s the DIV
fference?
divs
are generic HTML elements
that stand for division
of content, you can think of them as boxes of content. Spans
are like divs, but for text content.
Check out Mozilla Developer Network for more information:
After the <div id="contents"></div>
element, add a new <div></div>
element, and give it an ID attribute of the_map
like the follwing:
Our current index.html
should look like the following:
Prepping the CSS¶
Let’s incorporate our new div
that has the id
ofthe_map
into our CSS now.
Adding the_map
container¶
#the_map
tells this CSS selector to look for anID
calledthe_map
height
defines how tall thisdiv
should be,80vh
means 80% of the vertical height.- We are going to name this
grid-area
the following:main_map
it will be referenced in the.main
class, but you can name it whatever you want; just be consistent.
Why height: 80vh;
?
For Leaflet’s map to show, it must have a height defined in order to show up. So you can play around with the height in your assignments, but be sure to include some height!!
Putting the_map
id
into our main class
¶
Find our previous CSS style for the main-content
it should be a class called main
. Remember, because it is a class
it starts with a .
NOT a #
like IDs do!
IDs vs. Classes
- IDs: There can only be ONE unique ID on a HTML page and in CSS you refer to it with a
#
, like#the_map
. - Classes: There can be multiple classes on an HTML page and in CSS you refer to it with a
.
Our CSS should look like the following:
body{
display: grid;
/* grid-template-columns: 1fr; */
grid-auto-rows: auto 1fr;
grid-template-areas: "header" "main_content" "footer";
background-color: aqua;
/* height: 100vh; */
}
header{
grid-area: header;
}
#footer{
grid-area: footer;
}
.main{
grid-area: main_content;
grid-template-areas: "content" "main_map";
display: grid;
}
#contents{
grid-area: content;
}
#the_map{
height:80vh;
grid-area: main_map;
}
With our container ready to go, open up the JavaScript file again and add the following Leaflet code template:
L.
is theLeaflet
class that allows us to use built-in Leaflet tools. The.
is like a chain of commands, but similar tocss
, Leaflet wants us to knowwhere our map is!
In the documentation, the map constructor expects an ID, which we calledthe_map
. We then usesetView
to set theLatitude (y)
,Longitude (x)
, andZoom
of the initial map.- We use a
L.tilelayer
class to add a basemap to our map. - We use the
L.marker
to add a point to our map. Notice there is no;
here because the code continues.
Can you see the usage of latitude and longitude in the code?
What is latitude and Longitude
What is L.map
and L.tile
?¶
L.map
is Leaflet’s lingo for its own mapping Application Programming Interface (API). Every API has its own unique language to utilize it. You can think of it as an adapter to plug in your smart phone or laptop! Each plug has a different way to connect! To learn more about Leaflet’s API visit here:
https://leafletjs.com/reference-1.7.1.html
⚽ Class Exercise #2 - Adding more markers¶
Tasks
- Add some new markers!
- Customize the initial map
- Optional: change the base map or add some
html
into the marker popups.
Looking at the code above a little bit, we can see some latitude/longitude pairs. Your task is to copy the marker code add more markers of your choosing.
Unique variable names
When you create new marker variables, you must give the marker variable a new name, like marker2
or you will simply override the previous marker!
To find latitude/longitude of coordinates, you can use this website or another tool or your choosing:
Optional: Not happy with the basemap?¶
See if you can switch the basemap out by visiting the following link and changing L.tileLayer
on your map:
🏁Checkpoint¶
Check to see if your code looks likes the following before moving on: