Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
	<title>FCC Weather App</title>
	<link rel="stylesheet" type="text/css" href="weather.css">
	<link href="https://fonts.googleapis.com/css?family=Raleway:300,400,700" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false" key="AIzaSyCaBmWik_Ak--fJIq110jHLOhHNF7h41GU"></script>
</head>
<body>
	<div class="container">
		<div id="white-text" class="jumbotron white-text">
			<h1 id="header1">FCC Weather App</h1>
		<div>
			<p>You location: 
				<br>
				<span id="userLocation"></span>
			</p>
		</div>
		<div id="temperature">
			<span id="tempNum"></span>
			<span id="degree"></span><span id="c_or_f"></span>
		</div>
		<div id="weather"></div>
		<div id="icon_img"></div>
		</div>

	</div>	

	<p id="signature">- developed by Kyle</p>
	<script type="text/javascript" src="weather.js"></script>
</body>
</html>
              
            
!

CSS

              
                .container{
	margin: 20px auto 0px auto;
	width: 50%;
}


#white-text{
	background-color: black;
	text-align: center;
	color: rgb(242, 242, 242);
	
}

#header1{
	font-family: 'Raleway', sans-serif;
	
}

#signature{
	margin: 0 auto;
	text-align: center;
}

strong{
	font-weight: 300;
	font-size: 2em;
}

#weather{
	font-size: 2em;
}

#temperature{
	font-size: 2em;
}
              
            
!

JS

              
                var body = document.querySelector("body");
var userLocation = document.querySelector("#userLocation");
var temperature = document.querySelector("#temperature");
var c_or_f = document.querySelector("#c_or_f");
var tempNum = document.querySelector("#tempNum");
var degree = document.getElementById("degree");
var weather = document.getElementById("weather");
var icon_img = document.getElementById("icon_img");
var map, infoWindow;
var lat, lng;
var geocoder;
var temp_C;
var temp_F;
var myUrl;

window.addEventListener("load", function(){
	console.log("page loaded");
	initialize();
	if (navigator.geolocation){
		navigator.geolocation.getCurrentPosition(success);

	} else {
		alert("Geolocation is not enabled!");
}
});


function success(position){
	lat = position.coords.latitude;
	lng = position.coords.longitude;
	userLocation.innerHTML = 'Latitude: ' + Math.round(lat) + "&#176" + '<br>' + 'Longitude: ' + Math.round(lng) + "&#176";
	codeLatLng(lat, lng);
	getWeather(lat, lng);
	toggleTempUnit();

} 

function initialize() {
  geocoder = new google.maps.Geocoder();
}

function codeLatLng(lat, lng) {
  var city = '';
  var country = '';
  var latlng = new google.maps.LatLng(lat, lng);
  geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
      if (results[0]) {
      	for (var i = 0; i < results[0].address_components.length; i++){
      			if (results[0].address_components[i].types[0] === "locality") {
		        city = results[0].address_components[i].long_name;
		    	} else if (results[0].address_components[i].types[0] === "country"){
		        country = results[0].address_components[i].long_name;
		    }     
		    }
	    console.log(city + ", " + country);
	    userLocation.innerHTML +=  '<br><strong>'+ city + ', ' + country + '</strong>';
    	}
        }
      }
    );
}

// get weather data
function getWeather(lat, lng){
	var xmlhttp = new XMLHttpRequest();
	myUrl = 'https://fcc-weather-api.glitch.me/api/current?lat=' + lat + '&lon=' + lng;
	console.log(myUrl);
	xmlhttp.onreadystatechange = function() {
	    if (this.readyState == 4 && this.status == 200) {
	        var myWeather = JSON.parse(this.responseText);
	        var main = myWeather.weather[0].main;
	        var icon = myWeather.weather[0].icon;
	        temp_C = Math.round(myWeather.main.temp);
	        temp_F = Math.round(temp_C*1.8 + 32);
	        tempNum.innerHTML = temp_C;
	        // document.getElementById("degree").innerHTML = "&#176";
	        degree.innerHTML = "&#176";
	        c_or_f.innerHTML = "C";
	       	weather.innerHTML = main;
	       	icon_img.innerHTML = "<img src=" + icon + "alt='icon'" + ">";

	    }
	};
	xmlhttp.open("GET", myUrl, true);
	xmlhttp.send(); 
}



function toggleTempUnit(){
c_or_f.addEventListener("click", function(){
	if (c_or_f.innerHTML === "C"){ //if the current unit is celsius
		tempNum.innerHTML = temp_F;
		c_or_f.innerHTML = "F";
	}
		else if (c_or_f.innerHTML === "F") { //if the current unit is Fahrenheit
			tempNum.innerHTML = temp_C;
			c_or_f.innerHTML = "C";
		}

		}
	
)
}
              
            
!
999px

Console