【问题标题】:Using Openweather JSON API,how to fetch the temperature?使用 Openweather JSON API,如何获取温度?
【发布时间】:2017-07-17 11:19:41
【问题描述】:

通过传递 Latitude, Longitude 使用 Openweather JSON API,我正在获取数据。我需要获取当前华氏温度

基于 Openweather 上提供的示例 api,我正在从现场读取数据:deg

例子:

“风”:{“速度”:5.1,“度”:310}

度数:310。

我正在阅读的值不正确。具体数值是多少,我需要读读度数。

【问题讨论】:

    标签: jquery json openweathermap


    【解决方案1】:

    代码示例

    我附近的天气是 35 度,反应是 32 度。

    let API_KEY = 'e0a3dfaead51bbda58049371909fe21f';
    
    function getWeather(latitude, longtitude) {
      $.ajax({
        url: 'http://api.openweathermap.org/data/2.5/weather',
        data: {
          lat: latitude,
          lon: longtitude,
          units: 'imperial',
          APPID: API_KEY
        },
        success: data => {
           console.log(data["main"]["temp"] + " F");
        }
      })
    }
    
    getWeather(40.863372, -74.113181);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    解释

    将单位设置为英制,以便默认温度以 Farenheight units=imperial 返回,当前温度位于 main.temp

    文档说这是一个示例响应:

    {
      "coord": {
        "lon": 145.77,
        "lat": -16.92
      },
      "weather": [{
        "id": 803,
        "main": "Clouds",
        "description": "broken clouds",
        "icon": "04n"
      }],
      "base": "cmc stations",
      "main": {
        "temp": 293.25,
        "pressure": 1019,
        "humidity": 83,
        "temp_min": 289.82,
        "temp_max": 295.37
      },
      "wind": {
        "speed": 5.1,
        "deg": 150
      },
      "clouds": {
        "all": 75
      },
      "rain": {
        "3h": 3
      },
      "dt": 1435658272,
      "sys": {
        "type": 1,
        "id": 8166,
        "message": 0.0166,
        "country": "AU",
        "sunrise": 1435610796,
        "sunset": 1435650870
      },
      "id": 2172797,
      "name": "Cairns",
      "cod": 200
    }
    

    当前天气在main:

    "main": {
      "temp": 306.15, //current temperature
      "pressure": 1013,
      "humidity": 44,
      "temp_min": 306, //min current temperature in the city
      "temp_max": 306 //max current temperature in the city
    },
    

    要设置温度单位,请使用units= 参数:

    api.openweathermap.org/data/2.5/find?q=London&units=imperial

    更多信息来自他们的docs

    还有你可以使用的所有参数here

    【讨论】:

    • 谢谢。我也一样。帝国和从主要选择温度。 306.45 F 或 293.25 F 不是正确的温度。温度通常是 2 位数......我正在测试的位置......温度约为 35 F
    • 也许你正在寻找摄氏温度的units=metric。你在哪个位置测试?? @goofyui
    • 我住在新泽西州。如果您在 openweather.org 中尝试任何城市名称,您可以看到 32.8 F 左右的天气
    • @goofyui 我也住在泽西岛!我刚刚使用英制对其进行了测试,得到了准确的结果。
    • 波特雷亚尔,对不起。我没有在链接中添加“ &units=imperial ”。这就是为什么我得到无效数据。现在它工作正常。感谢您的帮助
    猜你喜欢
    • 2022-01-21
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多