Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (65 sloc) 1.79 KB
<script>
/*
1. Declare variables containing the URL for the Open Weather Map
API, and your API Key
*/
var apiUrl = 'http://api.openweathermap.org/data/2.5/weather';
var apiKey = 'fa32f';
/*
2. Declare a function that is responsible for getting the weather
for a given coordinate
*/
function getWeather(coordinate, callback) {
var url = apiUrl +
'?appid=' + apiKey +
'&units=imperial' +
'&lon=' + coordinate.longitude +
'&lat=' + coordinate.latitude;
makeRequest(url, function (responseText) {
response = JSON.parse(responseText);
callback(response);
});
}
/*
3. Declare a function that is responsible for making the HTTP
requests
*/
function makeRequest(url, callback) {
var xhrObject = new XMLHttpRequest();
xhrObject.onload = function () {
callback(xhrObject.responseText);
};
xhrObject.open('GET', url, true);
xhrObject.send();
}
/*
4. Declare a function that is responsible for getting our user's
coordinate.
*/
function getUserCoordinates(callback) {
navigator.geolocation.getCurrentPosition(function (position) {
if (position.coords) {
callback(position.coords);
}
});
}
/*
5. Declare a function that is responsible for the mundane task of
printing the temperature onto the user's screen.
*/
function printWeather(weatherData) {
var currentTemperature = weatherData.main.temp;
console.log("The current temprature at your location is: " +
currentTemperature);
}
/*
6. Declare a function that is responsible for putting all of this
together (i.e. execute all the other functions in the correct
order), and is gonna be the entrance point to our application.
*/
function weatherForMyLocation() {
getUserCoordinates(function (coord) {
getWeather(coord, printWeather);
});
}
</script>