Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- window.onload = function(){
- navigator.geolocation.getCurrentPosition(function(position) {
- var lat;
- var long;
- var units = "imperial";
- //F and C display code in button
- var fc = "F";
- // Button to switch from F and C
- var fcButton = "<button id=\"fcButt\" class=\"text-center\" >" + fc + "</button>";
- lat = position.coords.latitude;
- long = position.coords.longitude;
- $("#temp").html(fcButton);
- // F to C converter function
- //API Call
- $.getJSON("https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + long + "&APPID=328c9f6d02e07b2232eced26ae88ac5b&units=" + units, function(data) {
- //Temperature and Weather pushed to HTML
- $("#temp").html(JSON.stringify(data.main.temp) + fcButton);
- $("#weather").html(JSON.stringify(data.weather[0].main));
- var weatherCond = data.weather[0].main;
- var tempTog = data.main.temp;
- $("#fcButt").click(function(){
- if(fc == "C") {
- fc = "F";
- units = "imperial";
- } else if(fc == "F") {
- fc = "C";
- units = "metric";
- }
- $.getJSON("https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + long + "&APPID=328c9f6d02e07b2232eced26ae88ac5b&units=" + units, function(newData) {
- data = newData;
- });
- $("#temp").html(JSON.stringify(data.main.temp) + fcButton);
- $("#fcButt").html(fc);
- });
- //Background changer
- if(weatherCond === "Clouds"){
- var body = document.getElementsByTagName('body')[0];
- body.style.backgroundImage = 'url(http://wallpapercave.com/wp/4W2pw5V.jpg)';
- } else if(weatherCond === "Clear") {
- body = document.getElementsByTagName('body')[0];
- body.style.backgroundImage = 'url(http://wallpapercave.com/wp/oWfnrb6.jpg)';
- } else if(weatherCond === "Rain") {
- body = document.getElementsByTagName('body')[0];
- body.style.backgroundImage = 'url(http://www.ukweatherforecast.co.uk/wp-content/uploads/2014/05/rain.jpg)';
- }
- });
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement