mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-07 23:16:52 +08:00
Cleaner weather code
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
angular.module('ionic.weather', ['ionic.weather.services', 'ionic.weather.directives'])
|
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() {
|
.filter('int', function() {
|
||||||
return function(v) {
|
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) {
|
this.getForecast = function(lat, lng) {
|
||||||
$scope.current = resp.current_observation;
|
Weather.getForecast(lat, lng).then(function(resp) {
|
||||||
Weather.getForecast(resp.location.lat, resp.location.lon, function(resp) {
|
|
||||||
console.log('Forecast', resp);
|
console.log('Forecast', resp);
|
||||||
$scope.forecast = resp.forecast.simpleforecast;
|
$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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,7 +1,52 @@
|
|||||||
angular.module('ionic.weather.services', ['ngResource'])
|
angular.module('ionic.weather.services', ['ngResource'])
|
||||||
|
|
||||||
.factory('Weather', function($resource, API_KEY) {
|
.factory('Geo', function($q) {
|
||||||
var baseUrl = 'http://api.wunderground.com/api/' + API_KEY;
|
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', {
|
var locationResource = $resource(baseUrl + '/geolookup/conditions/q/:coords.json', {
|
||||||
callback: 'JSON_CALLBACK'
|
callback: 'JSON_CALLBACK'
|
||||||
@ -21,25 +66,31 @@ angular.module('ionic.weather.services', ['ngResource'])
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
getForecast: function(lat, lng, cb) {
|
getForecast: function(lat, lng, cb) {
|
||||||
|
var q = $q.defer();
|
||||||
|
|
||||||
forecastResource.get({
|
forecastResource.get({
|
||||||
coords: lat + ',' + lng
|
coords: lat + ',' + lng
|
||||||
}, cb);
|
}, function(resp) {
|
||||||
},
|
q.resolve(resp);
|
||||||
|
}, function(httpResponse) {
|
||||||
getAtCurrentLocation: function(cb) {
|
q.reject(httpResponse);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return q.promise;
|
||||||
},
|
},
|
||||||
getAtLocation: function(lat,lng, cb) {
|
|
||||||
|
getAtLocation: function(lat, lng) {
|
||||||
|
var q = $q.defer();
|
||||||
|
|
||||||
locationResource.get({
|
locationResource.get({
|
||||||
coords: lat + ',' + lng
|
coords: lat + ',' + lng
|
||||||
}, cb);
|
}, function(resp) {
|
||||||
|
q.resolve(resp);
|
||||||
|
}, function(error) {
|
||||||
|
q.reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
return q.promise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user