Website Weather Integration

Here’s a quick tutorial on how to integrate your own, near real-time weather stats into your website. There are sites, such as AccuWeather (who should, in all honesty, change their name) and Weather Underground, that will do this for you, but I find that you’re 1) beholden to their style and 2) not very accurate. The Weather Underground one for example will say one thing on your site but when you click the sticker (that’s what they call it) it says a completely different thing; their mobile app does the same thing. As with any of these sites that provide weather information, unless you live right next to where they have their sensors set up (typically the closest airport), the readings are likely to be off. This tutorial will be using NOAA, so let’s get started.

What you’ll need first is your location, latitude and longitude. There are a number of sites that you can get that information from; this one works.

Next, you’ll need to download weather.js (right-click save-as) and change the lat & lon variables to your location.

 * Change to your location. The below is Washington D.C.
var lat = '38.907192';
var lon = '-77.036871';

Then add the following to the page you want to integrate weather into by adding a DIV element with the id of weather, like so:

<div id="weather"></div>

Towards the end of your page, before the <body> tag, add the following:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="weather.js"></script>
    $(document).ready(function() {
        // get initial weather
        // refresh every 15 minutes; 900 * 1000
        setInterval(function() { getWeather(); }, 900000);

The above will result in something like the below that will automatically update every 15 minutes.

The following CSS was used to style the current conditions icon:

#weather-icon {
    border: 1px solid #b0b0b0;

And that’s all I have to say about that.


Comment (1)

Don't hold back, tell me how you really feel