123

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <body id="myID" class="myId-normal">

<div class = "header">
  weatherYo!
</div>

<div class="centerBlock" id="out">
    <span class="cityText"><!-- Anytown, US--></span><br><br>
    <span class="temperatureText"><!-- 69 --></span><a href="javascript:;" class="tempUnitsTest"><!-- °F --></a><br>
    <span class="weatherConditions"><!--cloudy --></span><br>
    <img id="myImg" src="" alt=""> <!--src="http://icons.wxug.com/i/c/k/cloudy.gif"-->
</div>

<div class="footer1"> 
    weatherYo! detects your location and fetches current weather conditions from <a href="https://www.wunderground.com"><b>wu</b></a> api, yo!
  </div>
<div class="footer2">
  designed by <a href="https://codepen.io/gt9142b/"><b>patrick coffey</b></a>
</div>
</body>
            
          
!
            
              .myId-normal {
    background: #ffffff;
    color:black;
}
.myId-normal a {
    color: black;
    text-decoration: none;
}

.myId-alternate {
    background:#000033;
    color:#faebd7; /*off white;*/
}
.myId-alternate a {
    color:#faebd7;
    text-decoration: none;
}

.header {
  font-size: 36px;
  text-align:center;
  margin-top: 10px;
  margin-bottom: 36px;
}

.centerBlock {
    margin: auto;
    max-width:450px;
    font-size: 24px;
    text-align:center;   
  /*border: 1px solid #aaa; */
}

.myLocation {
  font-size: 18px;
}

.time {
  font-size: 18px;
}
.temperature {
  font-size: 18px;
}
.footer1 {
  max-width:200px;
  text-align:center;
  margin:50px auto 21px;
  font-size:0.8em; 
}

.footer2 {
  text-align:center;
  margin:0px auto 21px;
  font-size:0.8em; 
}
.cityText{}
.temperatureText{/* */}
.tempUnitsTest{}
.weatherConditions{}

            
          
!
            
              // Goals for this JavaScript
// 1) Get user's location.
// 2) Display user's location
// 3) Send user's location to openWeather API
// 4) Receive current temperature 
// 5) Display current temperature
// 6) Enable button to switch temp between F/C 
// 7) Get current time
// 8) Display current time
// 9) Fetch photo of appropriate time of day for this location.
      // tell google maps api your location,
//Nearby Search, Text Search, or Place Details request. The response to these //requests will contain a photos[] field if the place has related //photographic content.
// If there are no photos, revert to a stock image for the weather.
// If there are photos, pass the photoID to google to retrieve the image, 
// and set it as your background.
      // nearby places.
// 10) Display photo in background
// 11) Bonus: Refresh time in real time.
// 12) Bonus: Refresh temperature and background photo every 1 minute.
/** NOTE: uses jQuery for quick & easy DOM manipulation **/



var wuKey = "ecc303b65d4d2261";
var tempUnit = "°F";
var weatherTempC, weatherTempF;


//
// getWeather()   Will fetch the current weather conditions for lat,lng parameters
//                Some JSON request code inspired by 
//                https://github.com/google/maps-for-work-samples/blob/master/samples/maps/OpenWeatherMapLayer/index.html
// 
function getWeather(lat, lng) {
  //console.log("Entering getWeather()...");
  //console.log("   lat,lng: " + lat + "," + lng);

  //var requestString5 = "https://api.wunderground.com/api/ecc303b65d4d2261/conditions/q/37.776289,-122.395234.json";
  var requestString6 = "https://api.wunderground.com/api/" + wuKey + "/conditions/q/" + lat + "," + lng + ".json";
  //console.log("requestString6: " + requestString6);
  request = new XMLHttpRequest();
  request.onload = proccessResults;
  request.open("get", requestString6, true);
  request.send();
  //console.log("sent request. Await reply...");
}
// Take the JSON results and proccess them
function proccessResults() {
  //console.log("Entering processResults...");
  //console.log(this);
  var results = JSON.parse(this.responseText);
  //console.log("Results: ");
  //console.log(results);
  //console.log(results.current_observation.display_location);
  weatherTempF = results.current_observation.temp_f;
  weatherTempC = results.current_observation.temp_c;
  var weatherIconURL = results.current_observation.icon_url;
  var weatherCity = results.current_observation.display_location.full;
  var weatherConditions = results.current_observation.weather;
  //console.log("Current Observation: ");
  //console.log(results.current_observation);
  //console.log("Temp F: " + weatherTempF);
  //console.log("Temp C: " + weatherTempC);
  //console.log("City: " + weatherCity);
  //console.log("WeatherIconURL: " + weatherIconURL);
  outputWeather(weatherCity, weatherTempF, weatherIconURL, weatherConditions);
}

  var outputWeather = function(city, temp, weatherIconURL, weatherConditions) {
    //console.log("Entering outputWeather...");
    $('.cityText').text(city);
    $('.temperatureText').text(temp + " ");
    $('.tempUnitsTest').text(tempUnit);
    $('.weatherConditions').text(weatherConditions + "!");
    document.getElementById("myImg").src = weatherIconURL; //"http://icons.wxug.com/i/c/k/cloudy.gif";// weatherIconURL;
  };

//
// geoFindMe()  
// Determines the current location (lat/lng) of browser.
// most code courtesy of: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation
//
function geoFindMe() {
  //console.log('entering geoFindMe()...');
  var output = document.getElementById("out");

  if (!navigator.geolocation) {
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    getWeather(latitude, longitude);
  } // end success()

  function error() {
    output.innerHTML = "Unable to retrieve your location. Please enable My Location.";
  } // end error()

  //output.innerHTML = "<p>Locating…</p>";

  var options = {
    // enableHighAccuracy = true:  extra time or power to return a really accurate result.
    //                      false: quick (but less accurate) answer.
    // timeout = how long does the device have, in milliseconds to return a result.
    // maximumAge = maximum age for possible previously-cached position. 
    //              0 = must return the current position, not a prior cached position.
    enableHighAccuracy: true,
    timeout: 10000,
    maximumAge: 0
  };
  navigator.geolocation.getCurrentPosition(success, error, options);
} // end function geoFindMe() 

$('.tempUnitsTest').on('click', function() {
  //console.log("Current Temp Unit: " + tempUnit);
  if (tempUnit == "°F"){
    tempUnit = "°C";
    $('.temperatureText').text(weatherTempC + " ");
  }
  else{
    tempUnit = "°F";
    $('.temperatureText').text(weatherTempF + " ");
  }
  $('.tempUnitsTest').text(tempUnit);
});

// Detect if it's nighttime. If so, switch
// to a darker themed CSS
// Rudimentary for Northern Hemisphere for now.
function dayOrNightCSS(){
    var d = new Date();
    var n = d.getHours();
    //console.log('hour: ' + d);
    if (n < 7 || n >= 19){
      changeColour();
    }
}
// changes CSS used by body to alternate "night" mode
function changeColour() {
    var myDiv = document.getElementById('myID');
    myDiv.className = 'myId-alternate';
}
// not used at this time, but opposite of changeColour!
function resetColour() {
    var myDiv = document.getElementById('myID');
    myDiv.className = 'myId-normal';
}

$(document).ready(function() {
  //console.log("ready!");
  dayOrNightCSS();
  geoFindMe();
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console