HOW TO find weather information for a place based on its WOEID using YQL & jQuery
There are many free Weather APIs that can fetch your weather details for a location but if you had to fetch it based on WOEID, you can turn to YQL or the Yahoo! Query Language & Yahoo! Weather API. Interestingly, unlike other free Weather APIs, YQL can also return JSONP results over HTTPS.
Here's the code (from my first GitHub Gist) -
The default unit of measurement of the temperature in the results returned by the API, is Fahrenheit. You can specify "c" or "f" for the unit parameter to get results in Centigrade and Fahrenheit respectively. Note that lower-case "c" or "f" has to be used.
Also see:
Here's the code (from my first GitHub Gist) -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Weather Forecast using YQL</title> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script> | |
//Developer notes - http://mvark.blogspot.in/2013/03/how-to-find-weather-information-for.html | |
//use YQL Console to build query - http://developer.yahoo.com/yql/console/ | |
function getWeather(woeid, unit) { | |
var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid=" + woeid + " %20and%20u=%27" + unit +"%27&format=json&callback=cbfunc"; | |
$.getScript(url, function(data) { } ); | |
} | |
window.cbfunc = function(data){ | |
var forecast=''; | |
forecast = "<br>" + data.query.results.channel.description + "<br>"; | |
forecast += data.query.results.channel.item.description; | |
$("#weather").append(forecast); | |
}; | |
getWeather(2295420, "c"); //Bangalore temperature in Centigrade | |
getWeather(2459115, "f"); //New York temperature in Fahrenheit | |
getWeather(2490383, "f"); //Seattle temperature in Fahrenheit | |
/* JSONP response returned by YQL query | |
cbfunc({ | |
"query": { | |
"count": 1, | |
"created": "2013-03-24T09:42:39Z", | |
"lang": "en-US", | |
"results": { | |
"channel": { | |
"title": "Yahoo! Weather - Bangalore, IN", | |
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html", | |
"description": "Yahoo! Weather for Bangalore, IN", | |
"language": "en-us", | |
"lastBuildDate": "Sun, 24 Mar 2013 2:31 pm IST", | |
"ttl": "60", | |
"location": { | |
"city": "Bangalore", | |
"country": "India", | |
"region": "KA" | |
}, | |
"units": { | |
"distance": "km", | |
"pressure": "mb", | |
"speed": "km/h", | |
"temperature": "C" | |
}, | |
"wind": { | |
"chill": "32", | |
"direction": "80", | |
"speed": "16.09" | |
}, | |
"atmosphere": { | |
"humidity": "14", | |
"pressure": "982.05", | |
"rising": "0", | |
"visibility": "8" | |
}, | |
"astronomy": { | |
"sunrise": "6:20 am", | |
"sunset": "6:30 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 Bangalore, IN at 2:31 pm IST", | |
"lat": "12.96", | |
"long": "77.62", | |
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html", | |
"pubDate": "Sun, 24 Mar 2013 2:31 pm IST", | |
"condition": { | |
"code": "32", | |
"date": "Sun, 24 Mar 2013 2:31 pm IST", | |
"temp": "32", | |
"text": "Sunny" | |
}, | |
"description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/32.gif\"/><br />\n<b>Current Conditions:</b><br />\nSunny, 32 C<BR />\n<BR /><b>Forecast:</b><BR />\nSun - Clear. High: 33 Low: 18<br />\nMon - Sunny. High: 34 Low: 20<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Bangalore__IN/*http://weather.yahoo.com/forecast/INXX0012_c.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n", | |
"forecast": [ | |
{ | |
"code": "31", | |
"date": "24 Mar 2013", | |
"day": "Sun", | |
"high": "33", | |
"low": "18", | |
"text": "Clear" | |
}, | |
{ | |
"code": "32", | |
"date": "25 Mar 2013", | |
"day": "Mon", | |
"high": "34", | |
"low": "20", | |
"text": "Sunny" | |
} | |
], | |
"guid": { | |
"isPermaLink": "false", | |
"content": "INXX0012_2013_03_25_7_00_IST" | |
} | |
} | |
} | |
} | |
} | |
}); | |
*/ | |
</script> | |
</head> | |
<body> | |
<div id="weather"></div> | |
</body> | |
</html> |
The default unit of measurement of the temperature in the results returned by the API, is Fahrenheit. You can specify "c" or "f" for the unit parameter to get results in Centigrade and Fahrenheit respectively. Note that lower-case "c" or "f" has to be used.
Also see:
Comments
Post a Comment