-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (165 loc) · 5.93 KB
/
script.js
File metadata and controls
183 lines (165 loc) · 5.93 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//Get values from html page such as user chosen state or type of
//filtering they wish to use
const stateCode = document.getElementById("state");
const main = document.getElementById("main")
const content = document.getElementById("content")
let userChoice = document.getElementById("typeFilter");
let infoButton = document.getElementById("infoButton");
let info = document.getElementById("info");
let currentLat = "";
let currentLong = "";
//Show site info
infoButton.addEventListener("click", (e) => {
if (e.currentTarget.innerHTML === "Hide info") {
e.currentTarget.innerHTML = "Show info"
info.innerHTML = ""
} else {
e.currentTarget.innerHTML = "Hide info"
info.innerHTML = "This site leverages the eBird API, " +
"allowing users to view recent bird sightings by location. Users "+
"can see recent observations in a specific state, by entering a two letter "+
"US state code. Alternatively, users can choose a point on a map and see "+
"recent observations within 25 km of that point. " +
"The map is implemented via the leafletjs library."
}
})
//intermediate function that I will change to change page state
userChoice.addEventListener("change", (event) => {
if (event.target.value == "map") {
content.innerHTML = ""
let newMap = document.createElement("div");
newMap.setAttribute("id", "map");
content.appendChild(newMap)
//create map object for user to select lat/long
let map = L.map('map').setView([30.6, -96], 8);
let popup = L.popup()
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
let submitMap = document.createElement("button");
submitMap.setAttribute("id", "submitMap");
submitMap.innerHTML = "Submit coordinates";
content.appendChild(submitMap);
map.on('click', onMapClick);
function onMapClick(e) {
currentLat = e.latlng.lat.toString()
currentLong = e.latlng.lng.toString()
popup.setLatLng(e.latlng)
.setContent(`lat: ${currentLat}, long: ${currentLong}`)
.openOn(map);
}
submitMap.addEventListener("click", () => {
if (currentLat === "") {
let noCoords = document.createElement("p");
noCoords.setAttribute("id", "noCoords");
noCoords.innerHTML = "No coordinates selected";
content.appendChild(noCoords);
} else {
if (document.getElementById("noCoords")) {
content.removeChild(noCoords);
}
let wrong = document.createElement("p");
wrong.setAttribute("id", "wrong");
content.appendChild(wrong);
let indNames = document.createElement("ul")
indNames.setAttribute("id", "indNames")
content.appendChild(indNames);
getBirdDatCoords(currentLat, currentLong)
}
})
} else if (event.target.value === "stateCode") {
content.innerHTML = ""
let choice = document.createElement("div");
choice.setAttribute("id", "input");
content.appendChild(choice)
let choiceLabel = document.createElement("label");
choiceLabel.setAttribute("for", "state");
choiceLabel.innerHTML = "State code:"
choice.appendChild(choiceLabel)
let textInput = document.createElement("input");
textInput.setAttribute("type", "text");
textInput.setAttribute("id", "state");
textInput.setAttribute("name", "state");
choice.appendChild(textInput)
let submitButton = document.createElement("button");
submitButton.setAttribute("type", "button");
submitButton.setAttribute("id", "submit");
submitButton.innerHTML = "Submit"
choice.appendChild(submitButton)
let wrong = document.createElement("p");
wrong.setAttribute("id", "wrong");
choice.appendChild(wrong);
let indNames = document.createElement("ul")
indNames.setAttribute("id", "indNames")
choice.appendChild(indNames)
const stateSubmit = document.getElementById("submit");
stateSubmit.addEventListener("click", function () {
const regionString = "US-" + state.value.toUpperCase()
getBirdDat(regionString)
})
}
})
//Interact with ebird api
let myHeaders = new Headers();
myHeaders.append("X-eBirdApiToken", "q7m6fj472qht");
let requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
maxResults: "100"
};
async function getBirdDat(location) {
try {
const url = "https://api.ebird.org/v2/data/obs/" + location +
"/recent" +
"?maxResults=100"
const response = await fetch(url, requestOptions)
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json()
if (Object.keys(data).length === 0) {
wrong.innerHTML = "No such place"
} else {
wrong.innerHTML = ""
}
showNames(data)
}
catch (error) {
console.error(`Could not get products: ${error}`);
}
}
function showNames(recentBirds) {
let indNames = document.getElementById("indNames");
indNames.innerHTML = ""
for (let i = 0; i < recentBirds.length; i++) {
let li = document.createElement("li");
li.appendChild(document.createTextNode(`${i+1}: `));
bold = document.createElement('strong');
bold.appendChild(document.createTextNode(`${recentBirds[i].comName}`));
li.appendChild(bold)
li.appendChild(document.createTextNode(`. ${recentBirds[i].obsDt}`));
indNames.appendChild(li);
}
}
async function getBirdDatCoords(lat, long) {
try {
const url = `https://api.ebird.org/v2/data/obs/geo/recent?lat=` +
lat + `&lng=` + long
const response = await fetch(url, requestOptions)
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const data = await response.json()
if (Object.keys(data).length === 0) {
wrong.innerHTML = "No such place"
} else {
wrong.innerHTML = ""
}
showNames(data)
}
catch (error) {
console.error(`Could not get products: ${error}`);
}
}