cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html lang="en">
<head>
	<meta charset="utf-8">
	<meta content="IE=edge" http-equiv="X-UA-Compatible">
	<meta content="width=device-width, initial-scale=1" name="viewport">
	<!-- Bootstrap says:  The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->


	<title>Weather</title><!-- Bootstrap -->
	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<link href="main.css" rel="stylesheet"><!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
	<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	<!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
	<div class="container-fluid">
		<div class="row">
			<div class="col-xs-12 weather-info">
				<div class="waswell">
					<h1>Local Weather</h1>


					<h2 id="users-coords"><!-- user's location-->
					</h2>


					<div class = "weather-item" id="current-weather">
						<!-- weather-->
					</div>


					<div class = "weather-item"  id="temperature">
						<!-- temperature-->
					</div>


					<div aria-label="..." class="btn-group-xs" role="group">
						<button class="btn btn-default" id="celcius" type="button">Celcius</button> <button autofocus="true" class="btn btn-default" id=
						"farenheit" type="button">Farenheit</button>
					</div>


					<div  class = "weather-item" id="wind">
						<!--speed and direction -->
					</div>


					<div class = "weather-item"  id="sunrise-sunset">
						<!-- sunrise and sunset times -->
					</div>


					<div  class = "weather-item-image" id="weather-image">
						<!-- icon from openweather -->
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
	</script> <!-- Include all compiled bootstrap plugins (below), or include individual files as needed -->
	 
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
	</script> 
	<script src="weather.js">
	</script>
</body>
</html>
            
          
!
            
              .weather-info{
	
	text-align: center;
	margin: 5px;
}
.weather-item{
	padding: 5px 0px 5px 0px;
	
}
.waswell{
	margin: 10px 0px 10px 0px;
	background-color: #FFF7D3;
}
body{
	background-color: #090310;
}

            
          
!
            
               /*Get the weather and update the page
 *
 * API CALL:  api.openweathermap.org/data/2.5/weather?zip=33418,us&appid=9edbccf035e35ac9403515d616122550 //
 * Documentation:  http://openweathermap.org/current#current_JSON
 */
// Get user's location https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
var options = {
	enableHighAccuracy: false,
	timeout: 5000,
	maximumAge: 0
};

function success(pos)
{
	var crd = pos.coords;
	console.log('Your current position is:');
	console.log('Latitude : ' + crd.latitude);
	console.log('Longitude: ' + crd.longitude);
	console.log('More or less ' + crd.accuracy + ' meters.');
	// $("#users-coords").html("latitude: " + crd.latitude + " longitude: " + crd.longitude);
	getWeather(crd.latitude, crd.longitude); //pass the user's lat/long to the weather ajax request
};

function error(err)
{
	console.warn('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.getCurrentPosition(success, error, options);
//https://learn.jquery.com/ajax/jquery-ajax-methods/  Get the weather data
function getWeather(userLat, userLong)
	{
		$.ajax(
			{
				// The URL for the request. example:
				//url: "http://api.openweathermap.org/data/2.5/weather?lat=26.8&lon=-80.1&appid=9edbccf035e35ac9403515d616122550&units=imperial",
				url: "http://api.openweathermap.org/data/2.5/weather?lat=" + userLat+"&lon=" + userLong + "&appid=9edbccf035e35ac9403515d616122550&units=imperial",
				// Whether this is a POST or GET request
				type: "GET",
				// The type of data we expect back
				dataType: "json",
			})
			// Code to run if the request succeeds (is done);
			// The response is passed to the function
			.done(function(json)
			{
				$("#users-coords").html(json.name);
				$("#current-weather").text(json.weather[0].main + " " + json.weather[0].description);
				// get temperatue in degrees Farenheit, use button group to control conversion to Centigrade
				temperatureFarenheit = Math.round(json.main.temp);
				$("#temperature").text(temperatureFarenheit + " °F");
				$("#wind").text("Wind Speed:  " + Math.round(json.wind.speed) +
					" MPH Direction:  " + degToCompass(Math.round(json.wind.deg)));
				//transform a number into a date object
				var sunriseDT = new Date(json.sys.sunrise * 1000);
				var sunsetDT = new Date(json.sys.sunset * 1000);
				$("#sunrise-sunset").html("Sunrise: " + sunriseDT.toLocaleString() +
					"<br> Sunset: " + (sunsetDT.toLocaleString()));
				console.log("epoch to string " + sunriseDT.toLocaleString());
				var weatherIcon = "http://openweathermap.org/img/w/" + json.weather[0].icon +
					".png";
				$("#weather-image").html('<img src = ' + weatherIcon +
					' alt = "weather related image"><img>');
				console.log(weatherIcon + " icon" + json.weather[0].icon);
			})
			// Code to run if the request fails; the raw request and
			// status codes are passed to the function
			.fail(function(xhr, status, errorThrown)
			{
				alert("Sorry, there was a problem!");
				console.log("Error: " + errorThrown);
				console.log("Status: " + status);
				console.dir(xhr);
			})
			// Code to run regardless of success or failure;
			.always(function(xhr, status)
			{
				//alert( "The request is complete!" );
			});
	}
	//http://stackoverflow.com/questions/7490660/converting-wind-direction-in-angles-to-text-words

function degToCompass(num)
{
	var val = Math.floor((num / 22.5) + 0.5);
	var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
	return arr[(val % 16)];
}
var temperatureKelvin = 0;
$(document).ready(function()
{
	$('#celcius').on('click', function(e)
	{
		$("#temperature").text(Math.round((temperatureFarenheit - 32) * 5 / 9) +" °C");
	});
	$('#farenheit').on('click', function(e)
	{
		$("#temperature").text(temperatureFarenheit + " °F");
	});
}); //doc ready
            
          
!
999px
Loading ..................

Console