How to Get Current Weather via Web Services

16,508

Solution 1

There are numerous weather APIs out there that will look up the weather based on zip codes, or find the nearest one for a zip code. See this question.

The national weather service has a REST API. CTRL+F for zipcode:

Summarized Data for One or More Zipcodes: Returns DWML-encoded NDFD data for one or more zip codes (50 United States and Puerto Rico).

Here is a sample request


I'm not sure if that one has current conditions. If not, you can use the Wunderground API. First you use GeolookupXML to look up nearby weather stations based on the city (see the nearby_weather_stations element in the sample output), then you can use WXCurrentObXML to get the conditions at one of the weather stations.

Solution 2

It took some searching, but NOAA maintains a list of all the METAR weather stations, with their latitude and longitude coordinates, as well as direct links to their RSS and XML feed URLs. The list is stored in XML here: http://www.weather.gov/xml/current_obs/index.xml

You could use Google's or Yahoo's geocoding APIs to lookup the latitude/longitude of a place name (City, Address, Zipcode, etc.) and then search the weather stations list for the closest METAR station to the given location.

Share:
16,508
Brandon
Author by

Brandon

I write the code that makes the ladies scream.

Updated on June 25, 2022

Comments

  • Brandon
    Brandon almost 2 years

    I am attempting to get the current weather given a zip code or a set of latitude/longitude coordinates. It appears that best practice to do this (and how NOAA does it) is to get the XML feed for a weather station.

    Example: http://www.weather.gov/xml/current_obs/KEDW.xml

    The only problem is that NOAA doesn't provide a good way to find the closest weather station given a zip code or coordinates and I did not see any hosted web services out there that will provide this mapping.

    Does anyone know of any web services to get the nearest weather station given a zip code or coordinate input? If not, does anyone have any great solutions to look into that provide similar information as NOAA does but takes in a zip code or coordinates?

  • Brandon
    Brandon about 14 years
    Thanks for that first link. I think I am going to use some of the NWS REST calls to get forecasts and cache that data. For current, I am going to dynamically get the weather station identifier from Wunderground's API and store them in our DB as needed and based upon a City, State, Country, ZipCode, or Lat/Long pair, I can look up the weather station identifier and call Wunderground or NOAA passing in the identifier to make a quick call to get the current data I need.