-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
190 lines (159 loc) · 5.41 KB
/
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
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
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#map{
width:600px;
height:500px;
float:left;
border: grey solid 10px;
margin: 10px;
}
#input{
margin:10px;
}
#navbar {
width: 100vw;
height: 50px;
background-color: lightblue;
margin-bottom:10px;
padding:5px;
}
#navbar img {
display: block;
margin-top:5px;
width: 40px;
height: 40px;
margin: auto;
border-radius:20%;
}
</style>
<title>Agency</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href=https://unpkg.com/[email protected]/dist/leaflet.css integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<script src=https://unpkg.com/[email protected]/dist/leaflet.js integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
</head>
<body>
<nav id="navbar">
<img src="https://i.pinimg.com/564x/06/c4/f7/06c4f70ec5931e2342e703e8a3f0a253.jpg" alt="Logo image">
</nav>
<div class = "container">
<div id = "map" style = "align-content:center"></div>
<div class = "container">
<form><input id = "input" placeholder="Type in a City!"></input></form>
<button onclick = {executeByCity()} style = "margin:10px">Weather</button>
<div id = "outputs">
</div>
<div>
</div>
<script>
var marker;
var map = L.map('map').setView([0,0],1);
L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=QZOjQ55UDWGl5sQc2Z5A',{
attribution:'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>'
}).addTo(map);
map.on('click', function(e) {
var s = "http://api.weatherapi.com/v1/current.json?key=c722ececb1094322a31191318231606&q=";
s +=e.latlng.lat + ",";
s+=e.latlng.lng
execute(s);
});
function executeByCity(){
const location = document.getElementById("input").value;
console.log(location);
const s = "http://api.weatherapi.com/v1/current.json?key=c722ececb1094322a31191318231606&q="+location;
try{
execute(s);
}
catch(err){
alert("Please input a valid city");
console.log("got it");
};
}
function execute(s){
fetch(s)
.then((response)=>response.json())
.then((data) => {
console.log(data);
const div = document.createElement('div');
try{
map.removeLayer(marker);
}
catch(err){
console.log("none");
}
marker = L.marker([data.location.lat,data.location.lon]).addTo(map);
if(document.getElementById("output")){
document.getElementById("output").outerHTML = "";
}
div.id = 'output';
div.setAttribute("border","white solid 10px");
const str1 = data.location.name+", " + data.location.region;
const str2 = 'Temperature(F): '+data.current.temp_f+' ';
const str3 = data.current.condition.text;
switch(str3){
case 'Sunny':
{
const img = document.createElement('img');
img.setAttribute("src", "./day.svg");
div.appendChild(img);
break;
}
case 'Partly cloudy':
{
const img = document.createElement('img');
img.setAttribute("src", "./cloudy-day-1.svg");
div.appendChild(img);
break;
}
case 'Cloudy':
case 'Overcast':
{
const img = document.createElement('img');
img.setAttribute("src", "./cloudy.svg");
div.appendChild(img);
break;
}
case 'Light rain':{
const img = document.createElement('img');
img.setAttribute("src", "./rainy-5.svg");
div.appendChild(img);
break;
}
case 'Heavy rain':{
const img = document.createElement('img');
img.setAttribute("src", "./rainy-7.svg");
div.appendChild(img);
break;
}
case 'Light rain shower':
case 'Patchy rain':
case 'Patchy rain possible':{
const img = document.createElement('img');
img.setAttribute("src", "./rainy-4.svg");
div.appendChild(img);
break;
}
}
const text = document.createElement('h1');
text.innerHTML = str1;
const text1 = document.createElement('h3');
text1.innerHTML = str2;
const text2 = document.createElement('h4');
text2.innerHTML = str3;
// const text = document.createTextNode(str2);
// const text1 = document.createTextNode(str3);
div.appendChild(text);
div.appendChild(text1);
div.appendChild(text2);
const place = document.querySelector('#outputs');;
place.appendChild(div);
});
}
</script>
</body>
</html>