Gavin King
College of Computer and Information Science
Mobile Engineer @ Vimeo
ghking13@gmail.com
http://ghking.co
Our Server
Web Browser
For this experiment, our goal is to build a web app that predicts how much it's going to snow in a given area.
But how can we predict the weather?
https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE
{
latitude: 42.3601,
longitude: -71.0589,
timezone: "America/New_York",
offset: -5,
currently: {...},
minutely: {...},
hourly: {...},
daily: {
summary: "Mixed precipitation tomorrow through Wednesday, with temperatures rising to 40°F on Sunday.",
icon: "snow",
data: [
{
time: 1424754000,
summary: "Light snow starting in the evening.",
sunriseTime: 1424777367,
sunsetTime: 1424816908,
moonPhase: 0.21,
precipIntensity: 0.0005,
precipIntensityMaxTime: 1424836800,
precipProbability: 0.03,
precipType: "snow",
precipAccumulation: 3.159,
temperatureMin: 1.02,
temperatureMinTime: 1424768400,
temperatureMax: 23.86,
temperatureMaxTime: 1424836800,
apparentTemperatureMin: -11.35,
apparentTemperatureMinTime: 1424761200,
apparentTemperatureMax: 15.46,
apparentTemperatureMaxTime: 1424836800,
dewPoint: 9.82,
humidity: 0.87,
windSpeed: 6.69,
windBearing: 233,
cloudCover: 0.5,
pressure: 1020.52,
ozone: 400.82
},
{...},
{...},
{...},
{...},
{...},
{...},
{...}
]
},
alerts: [...],
flags: {...}
}
response.daily.data.precipAccumulation
Forecast.io
Web Browser
?
Don't do this.
Forecast.io
Web Browser
Our Server
The Server
$ npm install -g express
$ express howmuchsnow
var constants = require('./constants.js');
var express = require('express');
var app = express();
// Set a port for our application
app.set('port', (process.env.PORT || 5000))
// Listen on that port
app.listen(app.get('port'), function()
{
console.log("Node app is running on port " + app.get('port'))
})
// Create our API route
app.get('/forecast', forecast);
module.exports = {
FORECAST_API_KEY: "474jsj87474hfn8347fh8748hw8f7ef289",
ERROR_RESPONSE: {status: "error"}
}
constants.js
index.js
function forecast(req, res, next)
{
// Extract the parameters
latitude = req.query.latitude;
longitude = req.query.longitude;
// Send a request to the Forecast.io API
client = json.newClient("https://api.forecast.io/")
url = 'forecast/' + constants.FORECAST_API_KEY + "/" + latitude + "," + longitude
client.get(url, function(error, response, body)
{
if (response.statusCode == 200)
{
days = body.daily.data;
preicpAmount = 0
// Get the accumulation for the next 3 days including today
for (i = 0; i < 3; i++)
{
day = days[i]
if (day.precipType == "snow")
{
preicpAmount += day.precipAccumulation;
}
}
return res.json({status: "ok", amount: preicpAmount});
}
else
{
console.log(error);
return res.json(constants.ERROR_RESPONSE);
}
});
}
The Browser
getLocationAndShowSnowfall();
function getLocationAndShowSnowfall()
{
if (navigator.geolocation)
{
// Get the user's location
navigator.geolocation.getCurrentPosition(getSnowfall);
}
else
{
showError();
}
}
function getSnowfall(position)
{
lat = position.coords.latitude;
lon = position.coords.longitude;
// Make the request to our server
$.getJSON("forecast/", {latitude: lat, longitude: lon}, showSnowfall);
}
function showSnowfall(response)
{
if (response.status == "ok")
{
populateUserInterface(response.amount);
}
else
{
showError();
}
}