Loops and APIs
Adding the data from our survey into our mapplication!

Goals
- Be able to use loops and conditional statements in JavaScript
- Understand what an API is
- Add data from a Google Sheet into a website
Start by creating a week5
folder in your lab assignments repo.
Get ahead start
If you finished lab 4
, you can also copy the contents of your week_4
folder and skip the following setup section.
Starting template code for lab #5
index.html |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 | <!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<!-- hint: remember to change your page title! -->
<meta charset="utf-8" />
<link rel="shortcut icon" href="#">
<link rel="stylesheet" href="styles/style.css">
<!-- 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>
</head>
<body>
<header>
<!-- space for a menu -->
</header>
<div class="main">
<div id="contents">
<!-- Be sure to use your own survey here!!!!!!! -->
<iframe src="https://docs.google.com/forms/d/e/1FAIpQLScD0IOr_U4r0q4HlBkZ7olkA5OJpgInePF8DQbIrIWDeTm1jw/viewform?embedded=true" width="100%" height="100%" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>
</div>
<div id="the_map"></div>
</div>
<div id="footer">
Copyright(2022)
</div>
<script src="js/init.js"></script>
</body>
</html>
|
js/init.js |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 | // declare variables
let mapOptions = {'center': [34.0709,-118.444],'zoom':5}
// use the variables
const map = L.map('the_map').setView(mapOptions.center, mapOptions.zoom);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// create a function to add markers
function addMarker(lat,lng,title,message){
console.log(message)
L.marker([lat,lng]).addTo(map).bindPopup(`<h2>${title}</h2> <h3>${message}</h3>`)
return message
}
const dataUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSpOaH94y-oguAqbtcvZRyKdrEYiT1JOzW0jmmreznYS8THdQTYQ6cUB7J_68SZLgjpXbB_FY_nDf2A/pub?output=csv"
function loadData(url){
fetch(url)
.then(response => {
console.log(response)
return response
})
.then(data =>{
// do something with the data
})
}
// this is our function call to get the data
loadData(dataUrl)
|
styles/style.css |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 | body{
display: grid;
grid-auto-rows: auto 1fr;
grid-template-areas: "header" "main_content" "footer";
background-color: aqua;
}
header{
grid-area: header;
}
#footer{
grid-area: footer;
}
.main{
grid-area: main_content;
grid-template-columns: 1fr 1fr;
grid-template-areas: "main_map content";
display: grid;
}
#contents{
grid-area: content;
}
#the_map{
height:80vh;
grid-area: main_map;
}
|