首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取今天、后天和后天的天气数据?

如何获取今天、后天和后天的天气数据?
EN

Stack Overflow用户
提问于 2016-03-24 15:29:05
回答 1查看 3.2K关注 0票数 0

我正在使用这个api:http://openweathermap.org/api,但我不知道如何才能为今天,第二天和后天的数据。有什么建议,我可以和如何使用哪些参数?

我试过这样做,但我迷路了

代码语言:javascript
复制
 var weatherUrl = 'http://api.openweathermap.org/data/2.5/forecast?q=Zurich&APPID=08dbab0eeefe53317d2e0ad7c2a2e060&units=metric';
      $.getJSON(
        encodeURI(weatherUrl),
        function(data) {

          if(data !== null && data.list !== null) {
            var result = data,
                weather = {},
                compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'],
                image404 = 'https://s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png';
                console.log(result);
               for(var i = 0; i < result.list.length; i++){
                 weather.temp = Math.round(result.list[i].main.temp);

                 weather.code = result.list[i].weather[0].id;

                 weather.text = ucfirst(result.list[i].weather[0].description);
                 weather.date = result.dt_txt;
            }

            options.success(weather);
          } else {
            options.error('There was a problem retrieving the latest weather information.');
          }
        }
      );
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-24 15:42:36

看一看:致电16天/每日预测数据。在这里,您的代码根据您的需要进行了编辑。

代码语言:javascript
复制
function ucfirst(str) {
    var firstLetter = str.slice(0, 1);
    return firstLetter.toUpperCase() + str.substring(1);
}

var weatherUrl = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=Zurich&APPID=APP_ID&units=metric';
$.getJSON(
    encodeURI(weatherUrl),
    function (data) {

        if (data !== null && data.list !== null) {
            var result = data,
                weather = {},
                compass = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'],
                image404 = 'https://s.yimg.com/os/mit/media/m/weather/images/icons/l/44d-100567.png';
            console.log(result);
            var weathers = [];
            for (var i = 0; i < 3; i++) {
                weathers.push({
                    temp: Math.round(result.list[i].temp.day),
                    code: result.list[i].weather[0].id,
                    text: ucfirst(result.list[i].weather[0].description),
                    date: new Date(result.list[i].dt * 1000)
                });
            }
            console.log(weathers);
            var today = weathers[0];
            var tomorrow = weathers[1];
            var dayAfterTomorrow = weathers[2];
        } else {
            console.log('There was a problem retrieving the latest weather information.');
        }
    }
);
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36203968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档