#概况
目前获取天气API的第三方大都只能服务于一个或者几个国家, 如:百度,高德,谷歌等。 经查目前提供查询天气通用的接口只有雅虎。安卓或IOS可通过系统获取用户所在地的相关信息,可参考 https://www.jianshu.com/p/7ce4b21810e5 。
而web端大都只能根据IP获取用户的相关位置, 而提供此服务的商家大都是收费的,如:MAXMIND. 免费的IP接口查询服务商有 IPInfoDB。
通过客户端的JSONP请求拿到客户机器IP所在地的相关信息。用法如下:
Usage
For city precision, do a query with the following API (if you omit the IP parameter it will check with your own IP):
http://api.ipinfodb.com/v3/ip-city/?key=YOUR_API_KEY&ip=IP_V4_OR_IPV6_ADDRESS
For country precision (faster), do a query with the following API:
http://api.ipinfodb.com/v3/ip-country/?key=YOUR_API_KEY&ip=IP_V4_OR_IPV6_ADDRESS
Note: Supported both the http and https API query.
If you don’t have an API key yet, just register a free API key. You will need this API key to be able to use our APIs.
ipinfodb 的 API 参数```
{
statusCode: "OK",
statusMessage: "",
ipAddress: "219.133.226.93",
countryCode: "CN",
countryName: "China",
regionName: "Guangdong",
cityName: "Shenzhen",
zipCode: "518026",
latitude: "22.5455",
longitude: "114.068",
timeZone: "+08:00"
}
```
根据上述接口返回的数据可以进一步通过请求雅虎天气API接口获取对应城市的天气数据,如设置雅虎的TOKEN可参考 Yahoo OAuth 2.0 Guide
获取不同天气类型天气数据,可查看Yahoo weather 的说明文档 ,从说明文文档中可知,通过jsonp技术请求链接即可获取对应的数据 如:https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="nome, ak")&format=json。‘q=’的值是YQL(和SQL语法类似)查询语句,‘text=’后面的nome代表请求的城市,ak代表请求的国家代号。返回结果如下:
```
{
"query": {
"count": 1,
"created": "2018-04-24T05:40:47Z",
"lang": "zh-cn",
"results": {
"channel": {
"units": {
"distance": "mi",
"pressure": "in",
"speed": "mph",
"temperature": "F"
},
"title": "Yahoo! Weather - Nome, AK, US",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/",
"description": "Yahoo! Weather for Nome, AK, US",
"language": "en-us",
"lastBuildDate": "Mon, 23 Apr 2018 09:40 PM AKDT",
"ttl": "60",
"location": {
"city": "Nome",
"country": "United States",
"region": " AK"
},
"wind": {
"chill": "25",
"direction": "90",
"speed": "18"
},
"atmosphere": {
"humidity": "95",
"pressure": "991.0",
"rising": "0",
"visibility": "11.5"
},
"astronomy": {
"sunrise": "6:59 am",
"sunset": "11:2 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 Nome, AK, US at 09:00 PM AKDT",
"lat": "64.499474",
"long": "-165.405792",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/",
"pubDate": "Mon, 23 Apr 2018 09:00 PM AKDT",
"condition": {
"code": "26",
"date": "Mon, 23 Apr 2018 09:00 PM AKDT",
"temp": "33",
"text": "Cloudy"
},
"forecast": [
{
"code": "28",
"date": "23 Apr 2018",
"day": "Mon",
"high": "33",
"low": "28",
"text": "Mostly Cloudy"
},
{
"code": "5",
"date": "24 Apr 2018",
"day": "Tue",
"high": "36",
"low": "30",
"text": "Rain And Snow"
},
{
"code": "26",
"date": "25 Apr 2018",
"day": "Wed",
"high": "34",
"low": "32",
"text": "Cloudy"
},
{
"code": "16",
"date": "26 Apr 2018",
"day": "Thu",
"high": "31",
"low": "30",
"text": "Snow"
},
{
"code": "14",
"date": "27 Apr 2018",
"day": "Fri",
"high": "33",
"low": "28",
"text": "Snow Showers"
},
{
"code": "5",
"date": "28 Apr 2018",
"day": "Sat",
"high": "34",
"low": "33",
"text": "Rain And Snow"
},
{
"code": "16",
"date": "29 Apr 2018",
"day": "Sun",
"high": "34",
"low": "32",
"text": "Snow"
},
{
"code": "26",
"date": "30 Apr 2018",
"day": "Mon",
"high": "34",
"low": "31",
"text": "Cloudy"
},
{
"code": "26",
"date": "01 May 2018",
"day": "Tue",
"high": "33",
"low": "32",
"text": "Cloudy"
},
{
"code": "28",
"date": "02 May 2018",
"day": "Wed",
"high": "34",
"low": "32",
"text": "Mostly Cloudy"
}
],
"description": "\n
\nCurrent Conditions:\n
Cloudy\n
\n
\nForecast:\n
Mon - Mostly Cloudy. High: 33Low: 28\n
Tue - Rain And Snow. High: 36Low: 30\n
Wed - Cloudy. High: 34Low: 32\n
Thu - Snow. High: 31Low: 30\n
Fri - Snow Showers. High: 33Low: 28\n
\n
\nFull Forecast at Yahoo! Weather\n
\n
\n
\n]]>",
"guid": {
"isPermaLink": "false"
}
}
}
}
}
}
```
各个参数所表示的意思参考说明雅虎天气说明文当。 这里需要注意的是默认的单位,以及风向, 风速等,由于全部是数字,无法向百度天气API接口返回的天气预报那样直观,因此需要对 风向以及风速做一个转换处理,
风力对照表 风向角度对照表需要注意的其他问题:
温度转化,由于获取的温度是华氏温度,和摄氏度的转换关系为:
摄氏度=(华氏度-32)÷1.8
风速单位是mph(mph miles per hour),其转换关系为:
1千米(km)=0.621英里
1英里(mile)=1.609千米(km)
通过以上数据, web端可以通过IP定位当前设备所在城市,并把城市名称传给雅虎天气请求接口,可获取对应城市天气数据。
注: 免费的ipinfodb获取的数据是粗略的数据,请求结果有可能为空。需要准确的IP定位信息,可购买其收费服务,或者其他第三方的收费服务。
参考文章:
网友评论