-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.js
More file actions
194 lines (166 loc) · 6.47 KB
/
Copy pathLocation.js
File metadata and controls
194 lines (166 loc) · 6.47 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @file Location API
*/
/**
* Location API
* @class
* @classdesc Gets location and name of place
* @constructor
* @param {object} weather - Weather object.
* @param {number} [latitude = current location latitude] - Latitude of location, from where weather data will be acquired.
* @param {number} [longitude = current location longitude] - Longitude of location, from where weather data will be acquired.
*/
function Location(weather, latitude, longitude) {
/**
* {@link Weather} object
* @public
*/
this.weather = weather;
/**
* Name of the city of current location
*@public
*/
this.city;
/**
* Time when location was updated.
* @public
*/
this.timeOfLastLocation = new Date(); // time of last location update
//Setting latitude and longitude, if not in arguments start acquiring
if (latitude === undefined || longitude === undefined) {
this.latitude = 0;
this.longitude = 0;
this.acquireLocation();
} else {
setLocation(latitude,longitude);
}
setInterval(function () { this.updateLocation(); }.bind(this), 1000 * 10); //interval to check if enough time passed to update weather and location
}
/**
* Location prototype
*/
Location.prototype = {
constructor: Location,
/**
* Method called from 10 seconds timer
*/
updateLocation: function (){
//console.log("t" + (new Date().getTime() / 1000 / 60 - this.timeOfLastLocation.getTime() / 1000 / 60));
this.acquireLocation();
},
/**
* Starts getting current location
*/
acquireLocation: function () {
//console.log("updejtam poziicjo");
//try gps location
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
function (position) {
var toMinutes = 1 / 60000;
if ((new Date().getTime() *toMinutes - this.timeOfLastLocation.getTime() *toMinutes) > 30) {
this.setLocation(position.coords.latitude, position.coords.longitude);
console.log("NEW WEATHER BECAUSE OF TIME ELAPSED SINCE LAST UPDATE");
} else {
if (this.isNewUpdateRequired(position.coords.latitude, position.coords.longitude)) {
this.setLocation(position.coords.latitude, position.coords.longitude);
console.log("NEW LOCATION; BECAUSE TO FAR AWAY");
}
}
}.bind(this),
//if gps not enabled, ip tracking
function () {
this.acquireLocationIP();
}.bind(this));
//if gps not enabled, ip tracking
} else {
this.acquireLocationIP();
}
},
/**
* Acquires location from device ip address
*/
acquireLocationIP: function () {
console.log("Updejtam ip lokacijo");
//ajax ip location call
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
//alert(data);
if (this.isNewUpdateRequired(data.lat, data.lon)) {
this.setLocation(data.lat, data.lon);
}
}
}.bind(this);
xmlhttp.open("GET", "http://ip-api.com/json/", true);
xmlhttp.send();
},
/**
* Sets latitude and longitude from received data.
* @params {number} latitude - Latitdue of current location.
* @params {number} longitude - Longitude of current location.
*/
setLocation: function (latitude, longitude) {
this.latitude = latitude;
this.longitude = longitude;
this.timeOfLastLocation = new Date();//Save time when location was set
this.getLocationName();
},
/**
* Gets name of acquired location based on geolocation with reverse-geocoding API
*/
getLocationName: function () {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
//alert(data);
this.city = data.geonames[0].adminName1;
this.weather.acquireWeatherData();
} else if (xmlhttp.status == 404) {
this.weather.acquireWeatherData();
}
}.bind(this);
xmlhttp.open("GET", "http://api.geonames.org/findNearbyPlaceNameJSON?lat="+this.latitude+"&lng="+this.longitude+"&username=pevecyan", true);
xmlhttp.send();
},
/**
* Returns true if distance between old and new location is more than 1000 meters or else
* @params {number} latitude - Latitude of the new location.
* @params {number} longitude - Longitude of the new location.
* @returns {boolean}
*/
isNewUpdateRequired: function (latitude, longitude) {
var newLatitude = latitude * (Math.PI / 180);;
var newLongitude = longitude * (Math.PI / 180);;
var oldLatitude = this.latitude * (Math.PI / 180);;
var oldLongitude = this.longitude * (Math.PI / 180);;
//calculate distance between new and old location
var distance = Math.acos(Math.sin(newLatitude) * Math.sin(oldLatitude) +
Math.cos(newLatitude) * Math.cos(oldLatitude) * Math.cos(newLongitude - oldLongitude)) * 6375;
distance = Math.floor(distance * 1000000) / 1000;
if (distance == NaN) distance = 0;
//alert(distance);
//if distance equal/more than 1000m, update location and weather
if (distance >= 1000) {
//this.setLocation(position.coords.latitude, position.coords.longitude);
return true;
console.log("NEW LOCATION; BECAUSE TO FAR AWAY");
} else {
return false;
}
}
}