Getting our data in place!¶
Go ahead and start the live server to make sure that the code is up and running. In Firefox, open up the Debug Console
(Right Button then click inspect element OR press F12) and click on the array from last week:
Remember that in an JSON object, keys
are essentially the field names:
And values
are the contents:
Remember to access a value, you have to access the object
and then the key
using .
notation or the ['name of field']
notation.
For example, last week when we wanted the lat
data, we used data.lat
:
addMarker(data.lat,data.lng,data['OpenEnded'],data['Is your English your first language?'])
Prepping the div
s¶
🏃Warm-up: Add a div
for the survey¶
Before we do anything to new, let’s move our survey into its own div
!
Answer
<div id="contents">
<div id="theSurvey">
<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>
With the survey in its own div
now, we can then add a <div>
for our new content:
<div id="contents">
<div id="placeForButtons">
<div id="theSurvey">
<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>
Adding a function
again?¶
Now, let’s look at the location
key. We probably want to add the locations to our contents to see what locations or showing up, as some people might put the same location more than twice!
We probably want to make buttons from these locations soooo....
Warm-up question: Create buttons (call-back to lab #2)!!¶
Question
Which line should we put this create buttons function:
createButtons(lat,lng,location);
Answers
It should go in our addMarker()
function! So that when we add markers, the buttons
are added afterwards!
Add the line for the createButton()
function in our JavaScript file:
function addMarker(lat,lng,title,message){
// console.log(message)
L.marker([lat,lng]).addTo(map).bindPopup(`<h2>${title}</h2> <h3>${message}</h3>`)
createButtons(lat,lng,location)
return message
}
Refactor the addMarker()
¶
Notice how the addMarker()
has a lot of parameters now? Like, 4? That’s quite a lot, and will be harder to manage if we want to add more.
So, let’s pass in the whole Object
instead of pieces of the object like so:
addMarker(data)
Before proceeding to the exercise make sure your addMarker()
call looks like the following inside the processData()
function:
function processData(results){
console.log(results)
results.data.forEach(data => {
console.log(data)
addMarker(data)
})
}
⚽In-class Exercise #1 - Refactor the addMarker()
function¶
We are refactoring the addMarker()
function during our loop to take in the whole object as a parameter. How might we re-write our addMaker()
function to take in the object?
Tasks
- Re-write the
addMarker()
function to take in the data object from theforEach()
loop.
Answer
function addMarker(data){
// console.log(data.OpenEnded)
L.marker([data.lat,data.lng]).addTo(map).bindPopup(`<h2>${data.Location}</h2> <h3>${data.OpenEnded}</h3>`)
createButtons(data.lat,data.lng,data.location)
return data.OpenEnded
}
Important note!
-
Make sure you changed the
return message
line in the end of the function, otherwise it will break since itmessage
is not defined anywhere! -
Make sure we use
data
object! Which means making sure we use theobject
+.
+key
format.
Reminder about accessing object-keys
again¶
Remember, since our data is being stored in an object, the createButton()
function should look like this:
createButtons(data.lat,data.lng,data.location)
- Where we are accessing the
data
object
‘slat
,lng
, andlocation
.
Note
Your keys==
MUST always match your data ==object
, you can use the console to check!!
For example: If your survey spreadsheet has latitude
instead of lat
then the you MUST use data.latitude
And now, just like in lab 2, we are going to add buttons!
Here’s the createButton()
from lab 2:
function createButtons(lat,lng,title){
const newButton = document.createElement("button"); // adds a new button
newButton.id = "button"+title; // gives the button a unique id
newButton.innerHTML = title; // gives the button a title
newButton.setAttribute("lat",lat); // sets the latitude
newButton.setAttribute("lng",lng); // sets the longitude
newButton.addEventListener('click', function(){
map.flyTo([lat,lng]); //this is the flyTo from Leaflet
})
document.body.appendChild(newButton); //this adds the button to our page.
}
Wait.. it didn’t work? Well, that’s because we have to tweak a few things…
- Next we need to change the
document.body.appendChild(newButton)
to use thediv
that we created earlier!
Adding buttons to our div
¶
To address the second issue of targeting our div
, we need to utilize the JavaScript method of selecting Elements called:
getElementById()
Learn more about getElementById()
Just running the method doesn’t do anything, so we need to store it in a variable:
const spaceForButtons = document.getElementById('placeForButtons')
Remember the appendChild()
that adds content?
We will use that method to add our button to our spaceForButtons
variable that specifies the div
:
spaceForButtons.appendChild(newButton);
The final createButtons()
and addMarker()
functions should look like this:
function addMarker(data){
// console.log(data)
// these are the names of our lat/long fields in the google sheets:
L.marker([data.lat,data.lng]).addTo(map).bindPopup(`<h2>${data.Location}</h2> <h3>${data.OpenEnded}</h3>`)
// adding our create button function
createButtons(data.lat,data.lng,data.location)
return data.Location
}
function createButtons(lat,lng,title){
const newButton = document.createElement("button"); // adds a new button
newButton.id = "button"+title; // gives the button a unique id
newButton.innerHTML = title; // gives the button a title
newButton.setAttribute("lat",lat); // sets the latitude
newButton.setAttribute("lng",lng); // sets the longitude
newButton.addEventListener('click', function(){
map.flyTo([lat,lng]); //this is the flyTo from Leaflet
})
const spaceForButtons = document.getElementById('placeForButtons')
spaceForButtons.appendChild(newButton);//this adds the button to our page.
}
Horrah!!
Our current HTML structure¶
Here’s a quick schematic of our HTML and CSS:
HTML
└BODY
└.main
├#contents
│ ├#placeForButtons
│ └#survey
└#the_map
Check to see if your JavaScript and HTML code is similar to the following before moving on.
🏁Check point¶
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
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 36 37 |
|