Là một nhà phát triển web, lấy dữ liệu từ API
là điều bạn sẽ làm thường xuyên. Tìm nạp dữ liệu thời tiết là một cách hoàn hảo để khiến bạn bị ướt chân. Trong bài viết này, chúng tôi sẽ sử dụng các trình duyệt được xây dựng trong
function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
0 với JavaScript để lấy dữ liệu từ API của OpenWeatherMapOpenWeatherMap API
OpenWeatherMap có API rất tốt. Họ cung cấp một cấp miễn phí cho phép thực hiện tối đa 60 cuộc gọi mỗi phút cùng với dự báo 5 ngày và 3 giờ
Tạo tài khoản OpenWeather
Để bắt đầu, chúng tôi cần một tài khoản. Truy cập Open Weather để đăng ký
Tạo khóa
Sau khi đăng ký, một khóa sẽ tự động được tạo cho bạn. Các khóa API được truy cập trong cài đặt tài khoản của bạn. Khóa này sẽ được sử dụng khi chúng tôi tìm nạp thông tin thời tiết, hoạt động như một mã định danh để OpenWeatherMap biết ai đang yêu cầu dữ liệu
điểm cuối
Lệnh gọi API mà chúng ta sẽ sử dụng có một vài tùy chọn khác nhau để nhận thông tin thời tiết. Để kiểm tra các cuộc gọi này trong trình duyệt của bạn, hãy lấy
function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
1 của bạn và thêm nó vào function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
2 dưới dạng tham số function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
3 được hiển thị bên dướitheo ID thành phố [Được khuyến nghị]
https. //api. bản đồ thời tiết mở. tổ chức/dữ liệu/2. 5/weather?id=6167865&appid={yourkey}
Lấy thông tin thời tiết hiện tại bằng cách sử dụng
function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
4 rõ ràng. Một danh sách các thành phố có sẵn ở đâytheo tên thành phố
https. //api. bản đồ thời tiết mở. tổ chức/dữ liệu/2. 5/thời tiết?q=Toronto,CA&appid={yourkey}
Trong đó
function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
5 là thành phố mong muốn của chúng tôi, cùng với mã quốc gia tùy chọn ở định dạng ISO 3166theo Vĩ độ, Kinh độ
https. //api. bản đồ thời tiết mở. tổ chức/dữ liệu/2. 5/thời tiết?lat=43. 700111&lon=-79. 416298&appid={keykey}
Có thêm tài liệu trên trang tài liệu của OpenWeatherMap
HTML
Khá đơn giản cho đến nay, bây giờ chúng ta hãy bắt tay vào một số mã. Tạo một tệp mới có tên
function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
6 trong trình chỉnh sửa yêu thích của bạn và bắt đầu với HTML sau
Current Weather
// future javascript
API tìm nạp
Để có được dữ liệu thực tế, chúng tôi sẽ sử dụng Fetch API. Nếu bạn chưa sử dụng tìm nạp trước đây thì đó là một cách để thực hiện các yêu cầu nhận dữ liệu đơn giản như API. Nó có hỗ trợ trình duyệt tốt với Chrome, Firefox và Edge tuy nhiên nó không hoạt động với Internet Explorer
Tham khảo thêm thông tin chi tiết về
Tìm thời tiết
Fetch API rất đơn giản. Nó hoạt động với một chức năng gọi là
function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
0 lấy function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
2 của bạn làm tham số duy nhất. Phản hồi ban đầu về mặt kỹ thuật là một luồng nội dung, vì vậy, để chuyển đổi nó, chúng tôi gọi sử dụng một function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
9 để trả về phản hồi {
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
0. Bọc nó trong một chức năng và bạn có quả bóng thời tiết của riêng mìnhfunction weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
console.log[data];
}]
.catch[function[] {
// catch any errors
}];
}
window.onload = function[] {
weatherBalloon[ 6167865 ];
}
Đặt cái này bên trong thẻ
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
1 và mở {
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
2 của bạn và bạn nếu bạn thấy phản hồi sau đây bạn có thời tiết[Nhấn F12 trong Chrome/Firefox/Edge để mở bảng điều khiển dành cho nhà phát triển]
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
Hiển thị thời tiết
Đối với một lập trình viên, một cấu trúc JSON được tổ chức độc đáo có vẻ đẹp riêng của nó, nhưng những người khác có thể không đồng ý. Chúng tôi thực sự cần đưa dữ liệu này lên trang theo cách có ý nghĩa
Đánh dấu thời tiết
Đã đến lúc thêm một vài thẻ html vào trang sẽ chèn dữ liệu vào. Trong cơ thể của bạn thêm trình giữ chỗ sau
Bây giờ đánh dấu của chúng tôi đã sẵn sàng, chúng tôi cần chèn dữ liệu thời tiết mà chúng tôi đã tìm nạp ở trên vào trình giữ chỗ
vẽThời tiết
Để làm điều này, chúng ta sẽ tạo một hàm
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
3 mới, thêm hàm này vào dưới hàm {
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
4 của bạn. Điều này sẽ lấy dữ liệu thời tiết làm tham số và chèn nó vào các phần tử div mô tả, tạm thời và vị trífunction drawWeather[ d ] {
var celcius = Math.round[parseFloat[d.main.temp]-273.15];
var fahrenheit = Math.round[[[parseFloat[d.main.temp]-273.15]*1.8]+32];
document.getElementById['description'].innerHTML = d.weather[0].description;
document.getElementById['temp'].innerHTML = celcius + '°';
document.getElementById['location'].innerHTML = d.name;
}
Hầu hết dữ liệu là thay thế trực tiếp, ngoại trừ nhiệt độ. OpenWeatherMap cung cấp cho chúng tôi nhiệt độ theo Kelvin, vì chúng không lộn xộn, vì vậy chúng tôi có hai chuyển đổi khác nhau đang diễn ra
sử dụng drawWeather
Tất nhiên chức năng là một nửa trận chiến. Chúng ta chưa gọi nó từ hàm
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
4. Thay vì bảng điều khiển. nhật ký, cuộc gọi {
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
3function weatherBalloon[ cityID ] {
var key = '{yourkey}';
fetch['//api.openweathermap.org/data/2.5/weather?id=' + cityID+ '&appid=' + key]
.then[function[resp] { return resp.json[] }] // Convert data to json
.then[function[data] {
drawWeather[data]; // Call drawWeather
}]
.catch[function[] {
// catch any errors
}];
}
Trang của bạn bây giờ trông như thế này
few clouds
9°
Toronto
Làm cho nó đẹp
Chúng ta có thể dừng lại ở đây nhưng điều đó sẽ thú vị biết bao. Chúng tôi cần nâng cấp trò chơi phông chữ của mình và thay đổi nền một chút tùy thuộc vào mô tả thời tiết. Thêm thẻ Google Font và
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
7 sau vào thẻ {
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
8 trong html của bạn
body {
font-family: 'Montserrat', sans-serif;
font-weight: 400;
font-size: 1.3em;
color: #fff;
text-shadow: .1em .1em 0 rgba[0,0,0,0.3];
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-image: linear-gradient[to right top, #99bbcb, #a5c7d7, #b1d4e2, #bde0ee, #c9edfa];
}
h1 {
margin: 0 auto;
font-size: 2.2em;
text-align: center;
color: #fff;
font-size: 5em;
}
body.sunny {
background-image: linear-gradient[to right top, #ff4e50, #ff713e, #ff932b, #ffb41d, #f9d423];
}
body.cloudy {
background-image: linear-gradient[to right top, #63747c, #71858e, #7f96a0, #8da8b2, #9bbac5];
}
body.rainy {
background-image: linear-gradient[to right top, #637c7b, #718e8c, #7ea09e, #8db2b0, #9bc5c3];
}
Chúng tôi đang sử dụng flexbox để căn giữa nội dung của chúng tôi theo chiều dọc và chiều ngang trên trang, nhưng chúng tôi cũng cần thêm chiều cao cho nội dung. Theo mặc định, phần thân chỉ lớn bằng nội dung của nó, vì vậy chúng tôi sẽ đảm bảo rằng nó là
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
9 tức là
0 chiều cao của khung nhìn
1 đổi lớp
Chức năng
{
"coord":{
"lon":-79.42,
"lat":43.7
},
"weather":[
{
"id":804,
"main":"Clouds",
"description":"overcast clouds",
"icon":"04n"
}
],
"base":"stations",
"main":{
"temp":292.15,
"pressure":1023,
"humidity":72,
"temp_min":291.15,
"temp_max":293.15
},
"visibility":14484,
"wind":{
"speed":5.7,
"deg":130
},
"clouds":{
"all":90
},
"dt":1537837200,
"sys":{
"type":1,
"id":3721,
"message":0.0039,
"country":"CA",
"sunrise":1537873704,
"sunset":1537916966
},
"id":6167865,
"name":"Toronto",
"cod":200
}
3 của chúng tôi bây giờ sẽ cần cập nhật để thay đổi hoán đổi lớp cơ thể giữa
3,
4 và
5. Chúng ta sẽ xem mô tả thời tiết được cung cấp bởi API
để tìm kiếm một vài từ khóa và chỉ cần điều chỉnh lớp học nếu chúng ta tìm thấy bất cứ điều gìfunction drawWeather[ d ] {
var celcius = Math.round[parseFloat[d.main.temp]-273.15];
var fahrenheit = Math.round[[[parseFloat[d.main.temp]-273.15]*1.8]+32];
var description = d.weather[0].description;
document.getElementById['description'].innerHTML = description;
document.getElementById['temp'].innerHTML = celcius + '°';
document.getElementById['location'].innerHTML = d.name;
if[ description.indexOf['rain'] > 0 ] {
document.body.className = 'rainy';
} else if[ description.indexOf['cloud'] > 0 ] {
document.body.className = 'cloudy';
} else if[ description.indexOf['sunny'] > 0 ] {
document.body.className = 'sunny';
}
}
bầu trời quang đãng phía trước
Bây giờ bạn có một trang thời tiết đang hoạt động. Chúng tôi chỉ làm trầy xước bề mặt trên dữ liệu có sẵn được cung cấp cho chúng tôi. Chúng tôi cũng nhận được thông tin về gió, mặt trời mọc/múi trời, độ ẩm, v.v. Nếu bạn thích mạo hiểm, hãy thêm một vài div nữa và bắt đầu thử với phần còn lại của dữ liệu. Nếu bạn chỉ muốn chơi xung quanh, hãy xem phiên bản hoàn chỉnh trên CodePen