Question

I'm getting the weather for a city using openweathermap.org.

The jsonp call is working and everything is fine but the resulting object contains the temperature in an unknown unit:

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

Here is a demo that console.log's the full object.

I don't think the resulting temperature is in fahrenheit because converting 290.38 fahrenheit to celsius is 143.544.

Does anyone know what temperature unit openweathermap is returning?

Était-ce utile?

La solution

It looks like kelvin. Converting kelvin to celsius is easy: Just subtract 273.15.

Looking at the API documentation, if you add &units=metric to your request, you'll get back celsius.

Autres conseils

Kelvin to Fahrenheit is:

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

I've noticed not all of the OpenWeatherApp calls read the units parameter if its passed in. (An example of this 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 is still returned.

You can change the unit to metric.

This is my code.

<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>

First Determine which Format do you want. Add Only &mode=json&units=metric after you send city in your BASE_URL. You will get dirrect Celsius value from the server.

Try this example

curl --location --request GET 'http://api.openweathermap.org/data/2.5/weather?q=Manaus,br&APPID=your_api_key&lang=PT&units=metric'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top