Unformatted text preview:

function Configuration var DEG c c for celsius f for fahrenheit var weatherDiv weather scroller scroller location p location Does this browser support geolocation if navigator geolocation navigator geolocation getCurrentPosition locationSuccess locationError else showError Your browser does not support Geolocation Get user s location and use OpenWeatherMap to get the location name and weather forecast function locationSuccess position try Retrive the cache var cache localStorage weatherCache JSON parse localStorage weatherCache var d new Date If the cache is newer than 30 minutes use the cache if cache cache timestamp cache timestamp d getTime 30 60 1000 Get the offset from UTC turn the offset minutes into ms var offset d getTimezoneOffset 60 1000 var city cache data city name var country cache data city country each cache data list function this holds a forecast object Get the local time of this forecast the api returns it in utc var localTime new Date this dt 1000 offset addWeather this weather 0 icon moment localTime calendar We are using the moment js library to format the date this weather 0 main b convertTemperature this main temp min DEG convertTemperature this main temp max DEG b Add the location to the page location html city b country b weatherDiv addClass loaded Set the slider to the first slide showSlide 0 else If the cache is old or nonexistent issue a new AJAX request var weatherAPI http api openweathermap org data 2 5 forecast lat position coords latitude lon position coords longitude callback getJSON weatherAPI function response Store the cache localStorage weatherCache JSON stringify timestamp new Date getTime getTime returns milliseconds data response Call the function again locationSuccess position catch e showError We can t find information about your city window console console error e function addWeather icon day condition var markup li img src assets img icons icon png p class day day p p class cond condition p li scroller append markup Handling the previous next arrows var currentSlide 0 weatherDiv find a previous click function e e preventDefault showSlide currentSlide 1 weatherDiv find a next click function e e preventDefault showSlide currentSlide 1 function showSlide i var items scroller find li Error handling functions function locationError error switch error code case error TIMEOUT showError A timeout occured Please try again break case error POSITION UNAVAILABLE showError We can t detect your location Sorry break case error PERMISSION DENIED showError Please allow geolocation access for this to work break case error UNKNOWN ERROR showError An unknown error occured break function convertTemperature kelvin Convert the temperature to either Celsius or Fahrenheit return Math round DEG c kelvin 273 15 kelvin 9 5 459 67 function showError msg weatherDiv addClass error html msg function use strict function setXML ajax url http query yahooapis com v1 public yql q select 20 20from 20weather forecast 20where 20woeid 3D2502265 diagnostics true datatype text success function data localStorage setItem xml content data getXML console log data parseJSON yweather 1 function getXML var xml localStorage getItem xml content weather text xml setXML jQuery


View Full Document
Download Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?