Here is my code:
var lat, lon
function getLocation(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
}
function noLocation() {
document.getElementById("#data").innerHTML = "";
alert("Sorry, we can not find your location.");
}
function setElement(data) {
document.getElementById("#data").innerHTML = data;
}
function getWeather() {
if (lat && lon) {
var apiKey = "2db9452f6f5d46d554721c08bfe1ab13";
var url = "https://api.darksky.net/forecast/";
var cors = "https://cors-anywhere.herokuapp.com/";
//var full = cors + url + apiKey + "/" + lat + "," + lon + "?exclude=currently,hourly,flags";
//var full = cors + url + apiKey + "/" + lat + "," + lon;
var full = url + apiKey + "/" + lat + "," + lon;
$.getJSON(full + "output=jsonp&callback=?", setElement);
}
}
$(document).ready(function () {
//unsupported browser alert
if (!navigator.geolocation) {
alert("Your Browser does not support Geolocation, sorry!")
}
else {
navigator.geolocation.getCurrentPosition(getLocation, noLocation);
//click-event handler
$("#weatherbtn").click(getWeather);
}
});