使用Dark Sky API显示每日天气

时间:2016-12-12 14:19:02

标签: jquery weather-api

我无法使用Dark Sky API让我的天气应用程序使用5天的预测工作。我能够使用"当前"来显示天气。作为我网址中的关键词,但我无法使用"每日"作为关键词。我知道这被标记为重复,但它正在寻找如何使用Dark Sky API使用JSON(而非JSONP)显示数据,因此预测显示本周的信息而不仅仅是1天。

有关如何使其发挥作用的任何想法?

这是我到目前为止的代码:

HTML

 <div class="container">
  <!-- Month and Year -->
  <div class="row">
    <div class="col-md-6"></div>
    <div class="col-md-6">
      <p id="monthNum"></p>
      <p id="year"></p>
    </div>
  </div>
  <!-- END Month and Year -->

  <!-- Weather Forecast -->
  <div id="weather">
    <div class="row">
      <div id="currentDay" class="col-md-2">
        <div class="thumbnail current-thumbnail">
          <div class="row">
            <div class="col-sm-12">
              <p class="time text-center"></p>
              <p class="location text-center"></p>
              <p class="tempFeel text-center"></p>
              <p class="humidity text-center"></p>
            </div>
          </div>
          <p class="text-uppercase text-center"><span class="forecastToday">Thu</span> <br /> <span class="forecastNumToday">14</span></p>
           <p class="icon text-center"></p>
           <p class="temp text-center"></p>
        </div>
      </div>
      <div class="col-md-2">
        <div class="thumbnail">
          <p class="text-uppercase text-center"><span class="forecastDay">Fri</span> <br /> <span class="forecastNum">15</span></p>
          <p class="icon-upcoming text-center"></p>
        </div>
      </div>
      <div class="col-md-2">
        <div class="thumbnail">
          <p class="text-uppercase text-center"><span class="forecastDay">Sat</span> <br /> <span class="forecastNum">16</span></p>
          <p class="icon-upcoming text-center"></p>
        </div>
      </div>
      <div class="col-md-2">
        <div class="thumbnail">
          <p class="text-uppercase text-center"><span class="forecastDay">Sun</span> <br /> <span class="forecastNum">17</span></p>
          <p class="icon-upcoming text-center"></p>
        </div>
      </div>
      <div class="col-md-2">
        <div class="thumbnail">
          <p class="text-uppercase text-center"><span class="forecastDay">Mon</span> <br /> <span class="forecastNum">18</span></p>
          <p class="icon-upcoming text-center"></p>
        </div>
      </div>
    </div>
  </div>
  <!-- END Weather Forecast -->
</div>

JS

    //Get the user's position
    if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {

    var lat = position.coords.latitude;
    var long = position.coords.longitude;

    //Dark Sky API key 
    var apiKey = "API-KEY";

    //Dark Sky URL 
    var weatherURL = "https://api.darksky.net/forecast/";

    //Cross-Origin URL to allow use on Chrome
    var corsURL = "https://cors-anywhere.herokuapp.com/";

    //Full Weather Forecast URL
    var fullURL = corsURL + weatherURL + apiKey + "/" + lat + "," + long + "?exclude=currently,hourly,flags";

    //jQuery JSON call to pull in temperature and icon information
    $.getJSON(fullURL, function(json) {

      for (i = 0; i < json.data.length; i++) {
        var temp = json.data.temperatureMax;
        //console.log(temp);
      }
      //Display the Current temp in Farenheit

      //$('.temp').html(temp + "&deg;F");

      var icon = json.daily.icon;
      //console.log(icon);
      $(".icon").html("<i class='wi wi-forecast-io-" + icon +"'></i>");
    });

1 个答案:

答案 0 :(得分:0)

快速解答:

要访问更多来自黑暗天空API的当前天气,请使用:

today = json['daily']['data'][0]['temperatureHigh']
tomorrow = json['daily']['data'][1]['temperatureHigh']
. . .

更改第三个[]中的数字以获取所需的每一天(例如[4]),范围从0到7。 在最后一个[]中,您可以使用关键字来访问与json不同的变量(例如“ temperatureLow”,“湿度”等)。

一些额外的内容:

基本上,问题应该是如何访问json文件。如果您输入浏览器URL,则请求(https://api.darksky.net/forecast/api_key/lat,lon?units=auto。您将获得一个巨大的json字符串。如果您解析它,那么应该清楚哪些元素适合什么。