Use Google weather API to query weather forecasts
Google weather API only supports querying by zip code in the United States. For example:
Http://www.google.com/ig/api? Hl = ZH-CN & weather = 94043
(94043 is the zip code of Mountain View city, California, USA)
In addition to the United States, you must use latitude and longitude coordinates as parameters to execute the Google weather API. For example:
Http://www.google.com/ig/api? Hl = ZH-CN & weather =, 3067133, 104019996
(3067213,10409706 is the longitude and latitude coordinates of Chengdu and mainland China)
Of course, you may also query the city name in Chinese pinyin. For example, the following is the weather in Beijing.
Http://www.google.com/ig/api? Hl = ZH-CN & weather = Beijing
To coordinate the longitude and latitude of other regions, you can use the countries provided by Google APICodeYou can query the list and corresponding city longitude and latitude coordinates. The following are the query parameters provided by Google API:
Http://www.google.com/ig/countries? Output = xml & HL = ZH-CN
(Query the code of all countries supported by Google and display it in ZH-CN Simplified Chinese)
Http://www.google.com/ig/cities? Output = xml & HL = ZH-CN & Country = Cn
C # example:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. xml;
Using system. net;
Namespace googleweatherapi_parser
{
Class Program
{
Static void main (string [] ARGs)
{
Int I = 0;
Xmlnodelist gwp_nodelist = googleweatherapi_parser (@ "http://www.google.co.uk/ig/api? Weather = Drammen "). selectnodes (" xml_api_reply/weather/current_conditions ");
Console. writeline ("current weather in Drammen:" + gwp_nodelist.item (I). selectsinglenode ("temp_c"). attributes ["data"]. innertext );
Console. Readline ();
}
Private Static xmldocument googleweatherapi_parser (string baseurl)
{
Httpwebrequest gwp_request;
Httpwebresponse gwp_response = NULL;
Xmldocument gwp_xmldoc = NULL;
Try
{
Gwp_request = (httpwebrequest) webrequest. Create (string. Format (baseurl ));
Gwp_request.useragent = @ "Mozilla/5.0 (windows; U; Windows NT 6.0; en-US; RV: 1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 ";
Gwp_response = (httpwebresponse) gwp_request.getresponse ();
Gwp_xmldoc = new xmldocument ();
Gwp_xmldoc.load (gwp_response.getresponsestream ());
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
Gwp_response.close ();
Return gwp_xmldoc;
}
}
}
Appendix: other weather APIs
Yahoo: http://developer.yahoo.com/weather/
Beijing weather (you can search for it)
Http://xml.weather.yahoo.com/forecastrss? P = chxx0008 & U = f
Domestic API:
Weather in Beijing
Http://weather.all2rss.com/weatherrss.asp? City = Beijing