Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.10 KB | None | 0 0
  1. $(document).ready(function(){
  2.     window.onload = function(){
  3.         navigator.geolocation.getCurrentPosition(function(position) {
  4.             var lat;
  5.             var long;
  6.             var units = "imperial";
  7.             //F and C display code in button
  8.             var fc = "F";
  9.             // Button to switch from F and C
  10.             var fcButton = "<button id=\"fcButt\" class=\"text-center\" >" + fc + "</button>";
  11.             lat = position.coords.latitude;
  12.             long = position.coords.longitude;
  13.  
  14.             $("#temp").html(fcButton);
  15.  
  16.             // F to C converter function
  17.  
  18.             //API Call
  19.             $.getJSON("https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + long + "&APPID=328c9f6d02e07b2232eced26ae88ac5b&units=" + units, function(data) {
  20.  
  21.                 //Temperature and Weather pushed to HTML
  22.                 $("#temp").html(JSON.stringify(data.main.temp) + fcButton);
  23.                 $("#weather").html(JSON.stringify(data.weather[0].main));
  24.                 var weatherCond = data.weather[0].main;
  25.                 var tempTog = data.main.temp;
  26.  
  27.                 $("#fcButt").click(function(){
  28.                     if(fc == "C") {
  29.                         fc = "F";
  30.                         units = "imperial";
  31.                     } else if(fc == "F") {
  32.                         fc = "C";
  33.                         units = "metric";
  34.                     }
  35.                    
  36.                     $.getJSON("https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + long + "&APPID=328c9f6d02e07b2232eced26ae88ac5b&units=" + units, function(newData) {
  37.                         data = newData;
  38.                     });
  39.                    
  40.                     $("#temp").html(JSON.stringify(data.main.temp) + fcButton);
  41.                     $("#fcButt").html(fc);
  42.                 });
  43.  
  44.                 //Background changer
  45.                 if(weatherCond === "Clouds"){
  46.                     var body = document.getElementsByTagName('body')[0];
  47.                     body.style.backgroundImage = 'url(http://wallpapercave.com/wp/4W2pw5V.jpg)';
  48.                 } else if(weatherCond === "Clear") {
  49.                     body = document.getElementsByTagName('body')[0];
  50.                     body.style.backgroundImage = 'url(http://wallpapercave.com/wp/oWfnrb6.jpg)';
  51.                 } else if(weatherCond === "Rain") {
  52.                     body = document.getElementsByTagName('body')[0];
  53.                     body.style.backgroundImage = 'url(http://www.ukweatherforecast.co.uk/wp-content/uploads/2014/05/rain.jpg)';
  54.                 }
  55.             });
  56.         }
  57.  
  58.     });
  59. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement