diff --git a/starters/weather/app.js b/starters/weather/app.js index 5c561aa201..221c817d17 100644 --- a/starters/weather/app.js +++ b/starters/weather/app.js @@ -1,6 +1,8 @@ angular.module('ionic.weather', ['ionic.weather.services', 'ionic.weather.directives']) -.constant('API_KEY', '1cc2d3de40fa5af0') +.constant('WUNDERGROUND_API_KEY', '1cc2d3de40fa5af0') + +.constant('FLICKR_API_KEY', '504fd7414f6275eb5b657ddbfba80a2c') .filter('int', function() { return function(v) { @@ -8,13 +10,34 @@ angular.module('ionic.weather', ['ionic.weather.services', 'ionic.weather.direct }; }) -.controller('WeatherCtrl', function($scope, Weather) { +.controller('WeatherCtrl', function($scope, Weather, Geo) { + var _this = this; - Weather.getAtCurrentLocation(function(resp) { - $scope.current = resp.current_observation; - Weather.getForecast(resp.location.lat, resp.location.lon, function(resp) { + this.getForecast = function(lat, lng) { + Weather.getForecast(lat, lng).then(function(resp) { console.log('Forecast', resp); $scope.forecast = resp.forecast.simpleforecast; + }, function(error) { + alert('Unable to get forecast'); + console.error(error); }); + }; + this.getCurrent = function(lat, lng) { + Weather.getAtLocation(lat, lng).then(function(resp) { + $scope.current = resp.current_observation; + _this.getForecast(resp.location.lat, resp.location.lon); + }, function(error) { + alert('Unable to get current conditions'); + console.error(error); + }); + }; + + Geo.getLocation().then(function(position) { + console.log('GOT LAT', position); + + _this.getCurrent(position.coords.latitude, position.coords.longitude); + }, function(error) { + alert('Unable to get current location: ' + error); }); + }); diff --git a/starters/weather/services.js b/starters/weather/services.js index 36f3f41c24..bb52061f6c 100644 --- a/starters/weather/services.js +++ b/starters/weather/services.js @@ -1,7 +1,52 @@ angular.module('ionic.weather.services', ['ngResource']) -.factory('Weather', function($resource, API_KEY) { - var baseUrl = 'http://api.wunderground.com/api/' + API_KEY; +.factory('Geo', function($q) { + return { + getLocation: function() { + var q = $q.defer(); + + navigator.geolocation.getCurrentPosition(function(position) { + q.resolve(position); + }, function(error) { + q.reject(error); + }); + + return q.promise; + } + }; +}) + +.factory('Flickr', function($q, $resource, FLICKR_API_KEY) { + var baseUrl = 'http://api.flickr.com/services/rest/' + + var flickrSearch = $resource(baseUrl + '?method=flickr.photos.search', { + safe_search: 1, + callback: 'JSON_CALLBACK', + api_key: FLICKR_API_KEY + }, { + get: { + method: 'JSONP' + } + }); + + return { + search: function(tags, lat, lng, cb) { + var q = $q.defer(); + + flickrSearch.get({ + }, function(val) { + q.resove(val); + }, function(httpResponse) { + q.reject(httpResponse); + }); + + return q.promise; + } + }; +}) + +.factory('Weather', function($q, $resource, WUNDERGROUND_API_KEY) { + var baseUrl = 'http://api.wunderground.com/api/' + WUNDERGROUND_API_KEY; var locationResource = $resource(baseUrl + '/geolookup/conditions/q/:coords.json', { callback: 'JSON_CALLBACK' @@ -21,25 +66,31 @@ angular.module('ionic.weather.services', ['ngResource']) return { getForecast: function(lat, lng, cb) { + var q = $q.defer(); + forecastResource.get({ coords: lat + ',' + lng - }, cb); - }, - - getAtCurrentLocation: function(cb) { - var _this = this; - - navigator.geolocation.getCurrentPosition(function(position) { - _this.getAtLocation(position.coords.latitude, position.coords.longitude, cb); - }, function(error) { - alert('Unable to get current location: ' + error); + }, function(resp) { + q.resolve(resp); + }, function(httpResponse) { + q.reject(httpResponse); }); + return q.promise; }, - getAtLocation: function(lat,lng, cb) { + + getAtLocation: function(lat, lng) { + var q = $q.defer(); + locationResource.get({ coords: lat + ',' + lng - }, cb); + }, function(resp) { + q.resolve(resp); + }, function(error) { + q.reject(error); + }); + + return q.promise; } } })