Created
December 12, 2015 14:38
-
-
Save evdokimovm/28056736a1e9f28d5b5d to your computer and use it in GitHub Desktop.
Weather. Current Temp. Celsius and Fahrenheit. JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Weather</title> | |
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script type="text/javascript" src="main.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="text-center"> | |
<h1 class="currentLocation"></h1> | |
<p class="currentTemperature" style="font-size: 1.5em"></p> | |
<a class="btn btn-lg btn-default btn-celsius">Get Celsius Temp</a> | |
<a class="btn btn-lg btn-default btn-fahrenheit">Get Fahrenheit Temp</a> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var getIP = 'http://ip-api.com/json/', | |
openWeatherMap = 'http://api.openweathermap.org/data/2.5/weather', | |
APPID = 'APPID'; | |
$.getJSON(getIP).done(function(location) { | |
$.getJSON(openWeatherMap, { | |
lat: location.lat, | |
lon: location.lon, | |
APPID: APPID | |
}) | |
.done(function(weather) { | |
var celsius = weather.main.temp - 273.15; | |
var fahrenheit = celsius * 1.8 + 32; | |
$('.currentLocation').text('Hello! Your current location is ' + location.city + ', ' + location.region + ', ' + location.country); | |
$('.btn-celsius').on('click', function() { | |
$('.currentTemperature').text('The current temperature in ' + location.city + ' is ' + celsius.toFixed(0) + ' degrees Celsius.'); | |
}); | |
$('.btn-fahrenheit').on('click', function() { | |
$('.currentTemperature').text('The current temperature in ' + location.city + ' is ' + fahrenheit.toFixed(0) + ' degrees Fahrenheit.'); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very helpful thanks!!!