• Stars
    star
    227
  • Rank 175,900 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

real weather for Javascript

Weather.js

Build Status npm GitHub license

About

Weather.js was designed to be a conclusive JavaScript weather library built around the OpenWeatherMap API (no affiliation). Since other providers format their output differently, currently OpenWeatherMap is the only source provider.

Weather.js is still in early development so expect changes and please contribute! Among the features I hope to incorporate:

  • historical weather information
  • API key usage (but there is a beta version!)
  • more data sources
  • more conversions!

Note: As stated above there are plans to add more providers in the future. If you have suggestions for other providers that you'd like to see please create a new issue with info about the provider and a link to the provider's API.

Weather.js was originally created by Noah Smith and is currently maintained by PallasStreams.

Install

Weather.js works in the browser and Node.js. Take your pick, For use in the browser, download the most recent version on GitHub. For use in Node, just install using your NPM package manager of choice. Currently Node has an old version of the library available but it will be updated soon.

npm install -g weather.js

Testing

To run the JavaScript unit tests run:

npm run test

Usage

At the moment you can access the current weather conditions and the forcast for any city. By default it will use the closes match as returned by OpenWeatherMap.

// API Key methods
var apiKey = '12345';
Weather.setApiKey( apiKey );
var tempApiKey = Weather.getApiKey();

// Language methods
var language = "de"; // set the language to German - libraries default language is "en" (English)
Weather.setLanguage( language );
var tempLanguage = Weather.getLanguage();

var cityId = '4393217';

// Get current weather for a given city
Weather.getCurrent( 'Kansas City', function( current ) {
    console.log(
        [ 'Currently:', current.temperature(), 'and', current.conditions() ].join( ' ' );
    );
} );

// Get the current weather for a given city using the city id
Weather.getCurrentByCityId( cityId, function( current ) {
    console.log(
        [ 'Currently:', current.temperature(), 'and', current.conditions() ].join( ' ' );
    );
} );

// Get the current weather for a given city using the latitude and longitude
var lat = 39.100,
    long = -94.579;
Weather.getCurrentByLatLong( lat, long, function( current ) {
    console.log(
        [ 'Currently:', current.temperature(), 'and', current.conditions() ].join( ' ' );
    );
} );

// Get the forecast for a given city
Weather.getForecast( 'Kansas City', function( forecast ) {
    console.log( 'Forecast High in Kelvin: ' + forecast.high() );
    console.log( 'Forecast High in Fahrenheit' + Weather.kelvinToFahrenheit( forecast.high() ) );
    console.log( 'Forecast High in Celsius' + Weather.kelvinToCelsius( forecast.high() ) );
} );

// Get the forecast for a given city using the city id
Weather.getForecastByCityId( cityId, function( forecast ) {
    console.log( 'Forecast High in Kelvin: ' + forecast.high() );
    console.log( 'Forecast High in Fahrenheit' + Weather.kelvinToFahrenheit( forecast.high() ) );
    console.log( 'Forecast High in Celsius' + Weather.kelvinToCelsius( forecast.high() ) );
} );

// Get the forecast for a given city using the latitude and longitude
var lat = 39.100,
    long = -94.579;
Weather.getForecastByLatLong( lat, long, function( forecast ) {
    console.log( 'Forecast High in Kelvin: ' + forecast.high() );
    console.log( 'Forecast High in Fahrenheit' + Weather.kelvinToFahrenheit( forecast.high() ) );
    console.log( 'Forecast High in Celsius' + Weather.kelvinToCelsius( forecast.high() ) );
} );