-
Notifications
You must be signed in to change notification settings - Fork 1
/
location.js
43 lines (37 loc) · 1.5 KB
/
location.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
namespace(function() {
window.coords = null
window.coordsChanged = function(onError, coords) {
if (window.coords === coords) return
// If this is the first time we set the coords since page load, it's not a legitimate location change.
// However, we still want to get location data, so that we can populate the settings page.
var isRealLocationChange = (window.coords != null)
window.coords = coords
window.setLocal('coords', coords)
window.getSunriseSunset(onError, function(placeName) {
if (isRealLocationChange) {
console.log('Location changed to "' + placeName + '", expiring weather data.')
window.setLocal('weatherExpires', 0)
}
})
}
window.requestLocation = function(onError, onSuccess) {
window.getLocal('coords', function(coords) {
if (coords) {
window.coordsChanged(onError, coords)
if (onSuccess) onSuccess(coords)
return
}
navigator.geolocation.getCurrentPosition(function(position) {
var coords = {'latitude': position.coords.latitude, 'longitude': position.coords.longitude}
window.coordsChanged(onError, coords)
if (onSuccess) onSuccess(coords)
}, function() {
httpGet('https://ipwhois.app/json/', 'discover your location', onError, function(position) {
var coords = {'latitude': parseInt(position.latitude), 'longitude': parseInt(position.longitude)}
window.coordsChanged(onError, coords)
if (onSuccess) onSuccess(coords)
})
})
})
}
})