23

我正在使用openweathermap.org獲取城市的天氣。如何計算openweathermap.org JSON中返回的攝氏溫度?

的JSONP調用工作,一切都很好,但由此產生的對象包含一個未知的裝置溫度:

{ 
    //... 
    "main": { 
     "temp": 290.38, // What unit of measurement is this? 
     "pressure": 1005, 
     "humidity": 72, 
     "temp_min": 289.25, 
     "temp_max": 291.85 
    }, 
    //... 
} 

這裏是一個演示是console.log的完整的對象。

我不認爲產生的溫度是華氏溫度,因爲將290.38華氏度轉換爲攝氏溫度是143.544

有誰知道什麼溫度單位openweathermap返回?

回答

75

看起來像kelvin。將開爾文轉換爲攝氏溫度很簡單:只需減去273.15。在the API documentation

而且最短一目瞭然告訴我們,如果你添加&units=metric你的要求,你會回來攝氏度。

+0

@TJCrowder這不是一個有點奇怪的默認設置嗎? – hitautodestruct

+3

@hitautodestruct:這是*我*,但是,我不是科學家。 :-) –

+4

Kelvin(http://en.wikipedia.org/wiki/Kelvin)是「國際單位制」的溫度單位。這是絕對的,基於物理學。零是「絕對零」。它看起來是一個相當自然的選擇,對我來說,「默認」... – MarcoS

6
+0

感謝您花時間回答! – hitautodestruct

+0

@spacebean它顯示如下錯誤:'{「cod」:401,「message」:「無效的API密鑰,請參閱http://openweathermap.org/faq#error401瞭解更多信息。」} –

1

您可以將單位更改爲公制。

這是我的代碼。

<head> 
    <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script> 
     <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script> 
     <style type="text/css">] 
     body{ 
      font-size: 100px; 

     } 

     #weatherLocation{ 

      font-size: 40px; 
     } 
     </style> 
     </head> 
     <body> 
<div id="weatherLocation">Click for weather</div> 

<div id="location"><input type="text" name="location"></div> 

<div class="showHumidity"></div> 

<div class="showTemp"></div> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#weatherLocation').click(function() { 
    var city = $('input:text').val(); 
    let request = new XMLHttpRequest(); 
    let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=[YOUR API KEY HERE]`; 


    request.onreadystatechange = function() { 
     if (this.readyState === 4 && this.status === 200) { 
     let response = JSON.parse(this.responseText); 
     getElements(response); 
     } 
    } 

    request.open("GET", url, true); 
    request.send(); 

    getElements = function(response) { 
     $('.showHumidity').text(`The humidity in ${city} is ${response.main.humidity}%`); 
     $('.showTemp').text(`The temperature in Celcius is ${response.main.temp} degrees.`); 
    } 
    }); 
}); 
</script> 

</body> 
相關問題