#概況
目前獲取天氣API的第三方大都只能服務(wù)于一個(gè)或者幾個(gè)國(guó)家荷辕, 如:百度,高德攀痊,谷歌等桐腌。 經(jīng)查目前提供查詢天氣通用的接口只有雅虎。安卓或IOS可通過(guò)系統(tǒng)獲取用戶所在地的相關(guān)信息苟径,可參考 http://www.reibang.com/p/7ce4b21810e5 案站。
而web端大都只能根據(jù)IP獲取用戶的相關(guān)位置, 而提供此服務(wù)的商家大都是收費(fèi)的,如:MAXMIND. 免費(fèi)的IP接口查詢服務(wù)商有?IPInfoDB棘街。
通過(guò)客戶端的JSONP請(qǐng)求拿到客戶機(jī)器IP所在地的相關(guān)信息蟆盐。用法如下:
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.
```
{
? ? ? ? ? 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"
? ? ? ? ? }
```
根據(jù)上述接口返回的數(shù)據(jù)可以進(jìn)一步通過(guò)請(qǐng)求雅虎天氣API接口獲取對(duì)應(yīng)城市的天氣數(shù)據(jù),如設(shè)置雅虎的TOKEN可參考 Yahoo OAuth 2.0 Guide
獲取不同天氣類型天氣數(shù)據(jù)遭殉,可查看Yahoo weather 的說(shuō)明文檔 石挂,從說(shuō)明文文檔中可知,通過(guò)jsonp技術(shù)請(qǐng)求鏈接即可獲取對(duì)應(yīng)的數(shù)據(jù) 如: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語(yǔ)法類似)查詢語(yǔ)句富岳,‘text=’后面的nome代表請(qǐng)求的城市,ak代表請(qǐng)求的國(guó)家代號(hào)拯腮。返回結(jié)果如下:
```
{
? "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"
? ? ? }
? ? }
? ? }
? }
? }
}
```
各個(gè)參數(shù)所表示的意思參考說(shuō)明雅虎天氣說(shuō)明文當(dāng)窖式。 這里需要注意的是默認(rèn)的單位,以及風(fēng)向动壤, 風(fēng)速等萝喘,由于全部是數(shù)字,無(wú)法向百度天氣API接口返回的天氣預(yù)報(bào)那樣直觀琼懊,因此需要對(duì) 風(fēng)向以及風(fēng)速做一個(gè)轉(zhuǎn)換處理阁簸,
需要注意的其他問(wèn)題:
溫度轉(zhuǎn)化,由于獲取的溫度是華氏溫度哼丈,和攝氏度的轉(zhuǎn)換關(guān)系為:
攝氏度=(華氏度-32)÷1.8
風(fēng)速單位是mph(mph miles per hour)启妹,其轉(zhuǎn)換關(guān)系為:
1千米(km)=0.621英里
?1英里(mile)=1.609千米(km)
通過(guò)以上數(shù)據(jù), web端可以通過(guò)IP定位當(dāng)前設(shè)備所在城市,并把城市名稱傳給雅虎天氣請(qǐng)求接口削祈,可獲取對(duì)應(yīng)城市天氣數(shù)據(jù)翅溺。
注: 免費(fèi)的ipinfodb獲取的數(shù)據(jù)是粗略的數(shù)據(jù),請(qǐng)求結(jié)果有可能為空髓抑。需要準(zhǔn)確的IP定位信息咙崎,可購(gòu)買其收費(fèi)服務(wù),或者其他第三方的收費(fèi)服務(wù)吨拍。
參考文章: