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 = "*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + Wcity + "%2C%20PK%22)&format=json&"; $.getJSON(yahooWeatherAPI, function (json) { var temp = Math.round(((( - 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