|
$(document).ready -> |
|
lat = undefined |
|
lon = undefined |
|
temp = undefined |
|
|
|
getLocationAndWeather = -> |
|
# If brwoser supports HTML5 geolocation then execute the getWeather function |
|
getWeather = (position) -> |
|
# Locate nearest city using Geocoder.ca API |
|
geocodeAPI = 'http://geocoder.ca/?latt=' + lat + '&longt=' + lon + '&reverse=1&allna=1&geoit=xml&corner=1&json=1' |
|
$.getJSON geocodeAPI, (data) -> |
|
$('#name').html 'The current weather in ' + data.city + ', ' + data.prov |
|
return |
|
# Using forcast api to get the current weather. |
|
Forcast_API_KEY = '464271a84e193070f9a5d159c9574296' |
|
forcastAPI = 'https://api.forecast.io/forecast/' + Forcast_API_KEY + '/' + lat + ',' + lon + '?callback=?' |
|
# Could only use an ajax request because the getJSON request was causing a cross domain error |
|
$.ajax |
|
url: forcastAPI |
|
dataType: 'jsonp' |
|
crossDomain: true |
|
success: (data) -> |
|
$('.default').remove(); |
|
$('#button-box').css('opacity', '1'); |
|
# Return summary |
|
$('#summary').html data.currently.summary |
|
# Return temperature |
|
temp = Math.floor(data.currently.temperature) |
|
$('#temp').html temp + '°F' |
|
#Switch up the weather icons based on the current condition |
|
switch data.currently.icon |
|
when 'clear-day' |
|
$('#icon').html '<i class="wi wi-day-sunny"></i>' |
|
when 'clear-night' |
|
$('#icon').html '<i class="wi wi-night-clear"></i>' |
|
when 'rain' |
|
$('#icon').html '<i class="wi wi-day-rain-mix"></i>' |
|
when 'snow' |
|
$('#icon').html '<i class="wi wi-day-snow"></i>' |
|
when 'sleet' |
|
$('#icon').html '<i class="wi wi-day-sleet"></i>' |
|
when 'wind' |
|
$('#icon').html '<i class="wi wi-day-windy"></i>' |
|
when 'fog' |
|
$('#icon').html '<i class="wi wi-day-fog"></i>' |
|
when 'cloudy' |
|
$('#icon').html '<i class="wi wi-day-cloudy"></i>' |
|
when 'partly-cloudy-day' |
|
$('#icon').html '<i class="wi wi-day-cloudy"></i>' |
|
when 'partly-cloudy-night' |
|
$('#icon').html '<i class="wi wi-night-cloudy"></i>' |
|
else |
|
$('#icon').html '<i class="wi wi-day-sunny"></i>' |
|
break |
|
return |
|
|
|
return |
|
|
|
# Throw errors if location is not defined |
|
|
|
showError = (error) -> |
|
switch error.code |
|
when error.PERMISSION_DENIED |
|
$('#error').html 'User denied the request for Geolocation.' |
|
when error.POSITION_UNAVAILABLE |
|
$('#error').html 'Location information is unavailable.' |
|
when error.TIMEOUT |
|
$('#error').html 'The request to get user location times out.' |
|
when error.UNKNOWN_ERROR |
|
$('#error').html 'An unknown error occurred.' |
|
return |
|
|
|
if navigator.geolocation |
|
navigator.geolocation.getCurrentPosition ((position) -> |
|
# Log the latitude and longitude |
|
lat = position.coords.latitude |
|
lon = position.coords.longitude |
|
# After coordinates are found then call the getWeather function |
|
getWeather() |
|
# Only call this function 6 times a day when the browser is open |
|
setInterval getWeather(), 144000000 |
|
return |
|
), showError |
|
else |
|
$('#location').html 'Geolocation is not supported by this browser.' |
|
return |
|
|
|
# Toggle the temperature between Fahrenheit and Celsius |
|
|
|
changeTemp = -> |
|
$('#button-c').click -> |
|
$('#button-c').addClass 'activated' |
|
$('#button-f').removeClass 'activated' |
|
$('#temp').html Math.floor((temp - 32) * .5555555) + '°C' |
|
return |
|
$('#button-f').click -> |
|
$('#button-f').addClass 'activated' |
|
$('#button-c').removeClass 'activated' |
|
$('#temp').html temp + '°F' |
|
return |
|
return |
|
|
|
getLocationAndWeather() |
|
changeTemp() |
|
return |
|
|
|
# --- |
|
# generated by js2coffee 2.1.0 |