Yahoo天气API实现根据经纬度获取地理和天气信息

目前获取天气主要是国内,想要实现国际化的天气通用查询接口,目前只有Yahoo实现了,Yahoo实现了一套自己的YQL,如果想要根据经纬度查询,需要将经纬度拼接入参作为YQL Query条件,可以在以下网址测试:

https://developer.yahoo.com/weather/

Yahoo天气API实现根据经纬度获取地理和天气信息

需要注意,YQL默认是以城市名称作为入参查询的,我们需要替换YQL为:

select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text="(latitude,longitude)")

以下是合肥某个地区的经纬度,代入YQL,点击Test,可以得到结果:

select * from weather.forecast where woeid in (SELECT woeid FROM geo.places WHERE text="(31.8189275,117.1919226)")


{
 "query": {
  "count": 1,
  "created": "2018-06-21T05:55:39Z",
  "lang": "zh-CN",
  "results": {
   "channel": {
    "units": {
     "distance": "mi",
     "pressure": "in",
     "speed": "mph",
     "temperature": "F"
    },
    "title": "Yahoo! Weather - Hefei, Anhui, CN",
    "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2127866/",
    "description": "Yahoo! Weather for Hefei, Anhui, CN",
    "language": "en-us",
    "lastBuildDate": "Thu, 21 Jun 2018 01:55 PM CST",
    "ttl": "60",
    "location": {
     "city": "Hefei",
     "country": "China",
     "region": " Anhui"
    },
    "wind": {
     "chill": "82",
     "direction": "60",
     "speed": "14"
    },
    "atmosphere": {
     "humidity": "69",
     "pressure": "1002.0",
     "rising": "0",
     "visibility": "16.1"
    },
    "astronomy": {
     "sunrise": "5:6 am",
     "sunset": "7:20 pm"
    },
    "image": {
     "title": "Yahoo! Weather",
     "width": "142",
     "height": "18",
     "link": "http://weather.yahoo.com",
     "url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
    },
    "item": {
     "title": "Conditions for Hefei, Anhui, CN at 02:00 PM CST",
     "lat": "31.86141",
     "long": "117.27562",
     "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2127866/",
     "pubDate": "Wed, 20 Jun 2018 02:00 PM CST",
     "condition": {
      "code": "28",
      "date": "Wed, 20 Jun 2018 02:00 PM CST",
      "temp": "82",
      "text": "Mostly Cloudy"
     },
     "forecast": [
      {
       "code": "30",
       "date": "21 Jun 2018",
       "day": "Thu",
       "high": "85",
       "low": "70",
       "text": "Partly Cloudy"
      },
      {
       "code": "30",
       "date": "22 Jun 2018",
       "day": "Fri",
       "high": "84",
       "low": "69",
       "text": "Partly Cloudy"
      },
      {
       "code": "47",
       "date": "23 Jun 2018",
       "day": "Sat",
       "high": "86",
       "low": "71",
       "text": "Scattered Thunderstorms"
      },
      {
       "code": "4",
       "date": "24 Jun 2018",
       "day": "Sun",
       "high": "89",
       "low": "76",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "25 Jun 2018",
       "day": "Mon",
       "high": "91",
       "low": "79",
       "text": "Thunderstorms"
      },
      {
       "code": "28",
       "date": "26 Jun 2018",
       "day": "Tue",
       "high": "89",
       "low": "79",
       "text": "Mostly Cloudy"
      },
      {
       "code": "4",
       "date": "27 Jun 2018",
       "day": "Wed",
       "high": "89",
       "low": "80",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "28 Jun 2018",
       "day": "Thu",
       "high": "90",
       "low": "80",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "29 Jun 2018",
       "day": "Fri",
       "high": "89",
       "low": "80",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "30 Jun 2018",
       "day": "Sat",
       "high": "90",
       "low": "79",
       "text": "Thunderstorms"
      }
     ],
     "description": "<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Mostly Cloudy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Thu - Partly Cloudy. High: 85Low: 70\n<BR /> Fri - Partly Cloudy. High: 84Low: 69\n<BR /> Sat - Scattered Thunderstorms. High: 86Low: 71\n<BR /> Sun - Thunderstorms. High: 89Low: 76\n<BR /> Mon - Thunderstorms. High: 91Low: 79\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2127866/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n<BR />\n]]>",
     "guid": {
      "isPermaLink": "false"
     }
    }
   }
  }
 }
}

这只是个简单的例子,具体使用方法还需要参照开发者网站介绍,而且通过官网说明,好像每个key每天只能限制2000次调用,如何合理的使用就是个问题了,可以给个思路作为参考,比如:本地实现接口缓存取到的天气数据,如果超过缓存时间再去获取最新的,相同城市的不同经纬度只获取一次(需要结合第三方经纬度判断城市来使用),使用多台小鸡服务器,使用多个key来延长限制次数,等等。

P.S. 顺便说下,Android的开发者,可以参考我的开源项目来根据经纬度获取当前经纬度的POI信息,传送门在下方。

bravecheng/GlobalPoi

标签:none