Demo entry 6630258

Weather app

   

Submitted by anonymous on Jul 08, 2017 at 12:53
Language: JavaScript. Code size: 3.4 kB.

$(function(){
  var C = false ;
  var apiData;
  
  backgroundImg = [
     "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQhhcUVwOp9ytUfgnFjjFm2peC1O-WRo-fco_46Me0oajDrg2mj",  //sunny 
    "https://c1.staticflickr.com/9/8297/7855831968_325f9ec074_k.jpg", //cloudy
"https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fwallpapercave.com%2Fwp%2FotV0PJe.jpg&imgrefurl=http%3A%2F%2Fwallpapercave.com%2Fweather-desktop-background&docid=7715epvkBhMKMM&tbnid=wjXMsw1YTTtP5M%3A&vet=10ahUKEwiKydPiofnUAhWoAMAKHdi8DX4QMwgtKAIwAg..i&w=1920&h=1080&bih=983&biw=1920&q=weather%20background%20images%20hd&ved=0ahUKEwiKydPiofnUAhWoAMAKHdi8DX4QMwgtKAIwAg&iact=mrc&uact=8", //storm
    "https://www.google.co.uk/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwjsrNmPovnUAhVHbBoKHY9bBR8QjRwIBw&url=https%3A%2F%2Fuk.pinterest.com%2Fpin%2F758926974676679550%2F&psig=AFQjCNEZXlEL2kUXlg30EGCfHTH0NA8Kyg&ust=1499588807992548", //rain
     "https://www.google.co.uk/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwjWm-2covnUAhWJ1RoKHUAQDjEQjRwIBw&url=http%3A%2F%2Fwallpapercave.com%2Fcool-background-hd&psig=AFQjCNEZXlEL2kUXlg30EGCfHTH0NA8Kyg&ust=1499588807992548", // other
    
,
  ];
  
  function displayTemp(F,C){
    if(C) return Math.round((F-32) *(5/9)) + "° C";
    return Math.round(F) + "° F" ;
    
  }
  
  function render(data,C){
    var currentWeather = data.current.condition.text; 
    var currentTemp = displayTemp(data.current.temp_f,C);
    var icon = data.current.condition.icon;
   //console.log(icon);
    
    $('#currentTemp').html(currentTemp);
    $('#currentWeather').html(currentWeather);
    var iconApi = "https://" + icon ;
    $('#currentTemp').prepend( '<img src=' + iconApi + '>');
 
   //console.log(currentWeather);
    
  
    
  }
   
  $.getJSON('https://freegeoip.net/json/').done(function(location){
    //console.log(location);
    $('#country').html(location.country_name);
    $('#city').html(location.city);
    $('#latitude').html(location.latitude);
    $('#longitude').html(location.longitude);
                       
   /*https://api.apixu.com/v1/current.json?key=584cfe187fb344868b3210009170707&q=*/ 
    //'https://crossorigin.me/https://api.darksky.net/forecast/7c8e28ee6acf02bdce3261cf546f6251/' + location.latitude+','+ location.longitude 
    $.getJSON('https://api.apixu.com/v1/current.json?key=584cfe187fb344868b3210009170707&q=' + location.latitude+','+ location.longitude , 
function(data)
               {  
    apiData = data;
     //console.log(apiData);
        render(apiData,C);
      
      $('#toggle').click(function(){
        C =!C
        render(data,C);
      })
      
      var id = data.current.condition.code,
          bgIndex,
          backgroundId=[1000,1003,1006,1009,1030,/*1063,1066/*,1069,1072,1087,1114,1117,1135,1147,1150,1153,1168,1171,1180,1183,1186,1189,1192,1195,1198,1201,1204,1207,1210,1213,1216,1219,1222,1225,1237,1240,1243,1246,1249,1252,1255,1258,1261,1264,1273,1276,1279,1282*/];
      
      backgroundId.push(id); 
      bgIndex = backgroundId.sort().indexOf(id);
      
     
    $('body').css('background-image','url(' + backgroundImg[bgIndex] + ')');
    
      console.log(backgroundImg[bgIndex]);
      console.log(bgIndex);
    })                                             
   })
})

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).