Skip to content

Instantly share code, notes, and snippets.

@ashlynnpai
Created September 4, 2017 22:19
Show Gist options
  • Save ashlynnpai/99cc77fda522ce02a4e8cc7ac9a169df to your computer and use it in GitHub Desktop.
Save ashlynnpai/99cc77fda522ce02a4e8cc7ac9a169df to your computer and use it in GitHub Desktop.
//Mozilla documentation for geolocation
function getWeather() {
var output = document.getElementById("out");
var picture = document.getElementById("icon");
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;
var url = "https://fcc-weather-api.glitch.me/api/current?lat=" + latitude + "&lon=" + longitude;
$.getJSON( url, function(data) {
output.innerHTML = data.weather[0].main;
var elem = document.createElement("img");
elem.src = data.weather[0].icon;
document.getElementById("icon").appendChild(elem);
var myTemp = data.main.temp
myTemp = Math.round(myTemp);
temperature.innerHTML = myTemp + " C";
});
}
function error() {
output.innerHTML = "Unable to retrieve your location";
}
navigator.geolocation.getCurrentPosition(success, error);
}
function toggleUnits() {
var getStr = document.getElementById("temperature");
var tempStr = getStr.innerHTML
var tempNum = tempStr.slice(0, -2);
var tempInt = parseInt(tempNum, 10);
var tempUnits = tempStr.slice(-1);
if(tempUnits == "C") {
tempInt = (tempInt * 1.8) + 32;
tempInt = Math.round(tempInt);
temperature.innerHTML = tempInt + " F";
}
else {
tempInt = (tempInt - 32) * (5/9);
tempInt = Math.round(tempInt);
temperature.innerHTML = tempInt + " C";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment