2017-08-26 25 views
0

我是新來的,我是編程初學者。
我需要天氣應用程序幫助。我使用的是用於顯示天氣的metaweather API,但我需要顯示多個位置的天氣。 這是我如何顯示只有一個城市的天氣,但我不知道如何通過更多的城市?!
請幫忙!在JavaScript/jQuery中有多個位置的天氣應用程序

這是代碼(HTML)

<main> 
     <section> 
      <div class="container"> 
       <div id="main_panel"> 
       <div class="row"> 
        <div class="col-xs-12 col-sm-6 col-md-4"> 
         <div class="app"> 
          <img class="img-responsive img-rounded" src="images/warszawa.jpg" alt="Warszawa"> 
          <span id="warsaw"> 
          <span class="location"> 
          Unknown 
           <i class="fa fa-map-marker"></i> 
           <span class="today">Today</span> 
          </span> 
          </span> 
          <span class="weather"> 
           <span class="temperature"> 
            0<sup>&deg;</sup> 
            <span class="unit">c</span> 
           </span> 
           <span class="weather-icon"></span> 
           <h3 class="weather-state"></h3> 
          </span> 
         </div> 
        </div> 
        <div class="col-xs-12 col-sm-6 col-md-4"> 
         <div class="app"> 
          <img class="img-responsive img-rounded" src="images/berlin.jpg" alt="Berlin"> 
          <span id="berlin"> 
          <span class="location"> 
          Unknown 
           <i class="fa fa-map-marker"></i> 
           <span class="today">Today</span> 
          </span> 
          </span> 
          <span class="weather"> 
           <span class="temperature"> 
            0<sup>&deg;</sup> 
            <span class="unit">c</span> 
           </span> 
           <h3 class="weather-state"></h3> 
           <span class="weather-icon"></span> 
          </span> 
         </div> 
        </div> 
        <div class="col-xs-12 col-sm-6 col-md-4"> 
         <div class="app"> 
          <img class="img-responsive img-rounded" src="images/lizbona.jpg" alt="Lizbona"> 
          <span id="location"> 
          Unknown 
           <i class="fa fa-map-marker"></i> 
           <span class="today">Today</span> 
          </span> 
          <span class="weather"> 
           <span id="temperature"> 
            0<sup>&deg;</sup> 
            <span class="unit">c</span> 
           </span> 
           <h3 class="weather-state"></h3> 
           <span class="weather-icon"></span> 
          </span> 
         </div> 
        </div> 
       </div> 
      </div> 
     </section> 
    </main> 

這裏,它是JavaScript的

var temperature = []; 

var cityName = 'warsaw'; 

var weatherSiteUrl = "http://cors-anywhere.herokuapp.com/https://www.metaweather.com/"; 
var weatherAPIUrl = weatherSiteUrl + "api/"; 
var cityLocation = weatherAPIUrl + "location/search/?query="; 
var iconUrl = "https://www.metaweather.com/"; 


function drawWeather() { 

$.getJSON(cityLocation + cityName, function(data) { 

    $.getJSON(weatherAPIUrl + 'location/' + data[0].woeid, function(data) { 
     $('.location').html(cityName + '<i class="fa fa-map-marker"></i>'); // Name of location 
     $('.weather-state').html(data.consolidated_weather[0].weather_state_name); //Weather state 
      temperature[0] = Math.floor(data.consolidated_weather[0].the_temp); 
     $('.temperature').html(temperature[0] + '<sup>&deg;</sup><span class="unit">c</span>'); // Temperature 
      var weatherImg = iconUrl + 'static/img/weather/' + data.consolidated_weather[0].weather_state_abbr + '.svg'; 
        $('.weather-icon').html('<img src=' + weatherImg + '>'); 

     }); 
    }); 
}; 

drawWeather(); 
+0

在此處閱讀API:https://www.metaweather.com/api/。你將需要更新你的位置,並進行另一個API調用結果。 – lscmaro

回答

0

代替硬編碼的 '華沙' 通過位置的功能

var temperature = []; 

var weatherSiteUrl = "http://cors-anywhere.herokuapp.com/https://www.metaweather.com/"; 
var weatherAPIUrl = weatherSiteUrl + "api/"; 
var cityLocation = weatherAPIUrl + "location/search/?query="; 
var iconUrl = "https://www.metaweather.com/"; 


function drawWeather(cityName) { 

$.getJSON(cityLocation + cityName, function(data) { 

    $.getJSON(weatherAPIUrl + 'location/' + data[0].woeid, function(data) { 
    $('.location').html(cityName + '<i class="fa fa-map-marker"></i>'); // Name of location 
    $('.weather-state').html(data.consolidated_weather[0].weather_state_name); //Weather state 
     temperature[0] = Math.floor(data.consolidated_weather[0].the_temp); 
    $('.temperature').html(temperature[0] + '<sup>&deg;</sup><span class="unit">c</span>'); // Temperature 
     var weatherImg = iconUrl + 'static/img/weather/' + data.consolidated_weather[0].weather_state_abbr + '.svg'; 
       $('.weather-icon').html('<img src=' + weatherImg + '>'); 

    }); 
}); 
}; 

然後,而不是drawWeather();運行功能使用drawWeather('warsaw');,drawWeather('berlin');,...

+0

嗨@ LW001感謝您的幫助 - 它應該可以工作,但是您能否幫助我找到將每個函數drawWeather與網站上的每個元素進行連接的方式?當使用drawWeather('warsaw')的正常運行函數;和drawWeather('berlin');第二個覆蓋第一個元素:/ – Artur