Cómo calculo la temperatura en celsius devuelta en openweathermap.org ¿JSON?


Estoy obteniendo el clima para una ciudad usando openweathermap.org.

La llamada jsonp está funcionando y todo está bien, pero el objeto resultante contiene la temperatura en una unidad desconocida:

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

Aquí hay una demostración que console.log es el objeto completo.

No creo que la temperatura resultante esté en fahrenheit porque convertir 290.38 fahrenheit a celsius es 143.544.

¿Alguien sabe qué unidad de temperatura está devolviendo openweathermap?

Author: John Slegers, 2013-10-20

4 answers

Se parece a kelvin. Convertir kelvin a celsius es fácil: Solo resta 273.15.

Y el breve vistazo en la documentación de la API nos dice que si agrega &units=metric a su solicitud, obtendrá celsius.

 90
Author: T.J. Crowder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-20 12:34:10

Que parece ser kelvin, pero puede especificar el formato que desea devolver para la temperatura, por ejemplo:

Http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=metric

O

Http://api.openweathermap.org/data/2.5/weather?q=London&mode=json&units=imperial

 8
Author: spacebean,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-20 12:36:29

Kelvin a Fahrenheit es:

(( kelvinValue - 273.15) * 9/5) + 32

He notado que no todas las llamadas a OpenWeatherApp leen el parámetro units si se pasa. (Un ejemplo de este error: http://api.openweathermap.org/data/2.5/group?units=Imperial&id=5375480,4737316,4164138,5099133,4666102,5391811,5809844,5016108,4400860,4957280&appid=XXXXXX) Kelvin sigue siendo devuelto.

 1
Author: Sara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-05 19:44:31

Puede cambiar la unidad a métrica.

Este es mi código.

<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>
 1
Author: Darragh Blake,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-15 15:01:51