/
home.controller.js
86 lines (77 loc) · 2.84 KB
/
home.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
angular.module('myApp')
.controller('HomeController', ['$scope', '$geolocation', 'openweathermapFactory', '$http',
function($scope, $geolocation, openweathermapFactory, $http) {
$scope.userDenied = false;
$scope.loading = false;
$scope.location = {};
// get the specified location by City Name
// List of 2,000 cities are provided.
$scope.requestCity = function() {
$scope.loading = true;
console.log($scope.location);
openweathermapFactory.getWeatherFromCitySearchByName({
q: $scope.location.name,
appid: "95fce1ea5c2190183b85f3e518de44cd"
}).then(function(data) {
$scope.loading = false;
$scope.position = data;
console.log(data);
$scope.positionReady = true;
}).catch(function(error) {
console.log(error);
toastr.error('Could not complete task', 'Some error happened. See console for details');
})
};
$scope.requestCoord = function() {
$scope.loading = true;
console.log($scope.location);
doWeather($scope.location);
$scope.positionReady = true;
}
doWeather = function(position) {
$scope.loading = true;
toastr.info('Going to fetch report', 'Hold on tight!');
// get the weather report
openweathermapFactory.getWeatherFromLocationByCoordinates({
lat: position.coords.latitude,
lon: position.coords.longitude,
appid: "95fce1ea5c2190183b85f3e518de44cd"
}).then(function(data) {
$scope.loading = false;
$scope.position = data;
console.log(data);
toastr.info('Weather report is ready');
}).catch(function(error) {
console.log(error);
toastr.error('Could not complete task', 'Some error happened. See console for details');
})
};
$geolocation.getCurrentPosition({
timeout: 6000
}).then(function(position) {
$scope.position = position;
console.log(position);
// start rendering <ng-map></ng-map>
// only after position above are available
$scope.positionReady = true;
// get the location with coordinates
doWeather(position);
})
.catch(function(error) {
console.log(error);
$scope.userDenied = true;
toastr.error('You denied access to Geolocation. Switching to manual mode.', 'No location available')
// only of 2,000 in number to speed up things.
$http.get('./assets/city.list.json')
.then(function(data) {
// console.log(data.data);
$scope.cities = data.data
toastr.info('List of cities finished loading', 'Cities are loaded.')
})
.catch(function(error) {
toastr.error('Could not complete task', 'Some error happened. See console for details');
console.log(error);
})
})
}
])