Wednesday, April 6, 2016

How to get city weather from Yahoo Weather API using Javascript

Hi There,

Yahoo provides a superb JavaScript API to get weather updates for a specific city. More you can read Yahoo weather API documentation on Yahoo official website. However i am putting here a sample JavaScript code that how you can get city weather from Yahoo weather dynamically.

JavaScript Code:

[code]var Wcity = "Gilgit"; ///Fetching Weather Report yahooWeatherAPI = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + Wcity + "%2C%20PK%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; $.getJSON(yahooWeatherAPI, function (json) { var temp = Math.round((((json.query.results.channel.item.condition.temp - 32) * 5) / 9));// + "°"; $('#currentWeather').text(temp); $('#weatherCity').text(Wcity); }); [/code]

HTML Code:

[code] [/code]

Try replacing the city with your desired city name or get the city name dynamically and use this ajax call in your function. It worked for me and hope work for you as well.

Please notice that in yahooWeatherAPI url above, i have highlighted PK as its the country code word for yahoo. You may have to change it according to your country. I am currently in Pakistan so it works fine for me.

Happy Coding :)

No comments:

Post a Comment