-
Notifications
You must be signed in to change notification settings - Fork 1
/
backgroundlocationindicator.js
51 lines (42 loc) · 1.6 KB
/
backgroundlocationindicator.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
/**
* Docs: https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Geolocation-property-showBackgroundLocationIndicator
*/
var locationIndicator = false;
function toggleIndicator() {
// Inverse current status
locationIndicator = !locationIndicator;
// Before we can change the indicator, we need to stop monitoring first.
stopLocationMonitor();
Ti.Geolocation.showBackgroundLocationIndicator = locationIndicator;
startLocationMonitor();
$.statusIndicator.text = locationIndicator ? 'visible' : 'indicator visible';
}
function hasPermission() {
// check for the right permission status
if (Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS)) {
$.permission.hide();
$.control.show();
startLocationMonitor();
} else if (Ti.App.Properties.getBool('askedLocationPermission', false)) {
alert('Gave wrong or no permission. Please give the always permission in the settings to demonstrate this feature');
// can't ask again on iOS, so don't show button
$.requestPermission.hide();
}
}
// run on start in case user enters page a second time
hasPermission();
function requestPermission() {
Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function (e) {
Ti.App.Properties.setBool('askedLocationPermission', true);
hasPermission();
});
}
function locationCallback(e) {
// got location here. We're not using the callback as we don't need it
}
function stopLocationMonitor() {
Ti.Geolocation.removeEventListener('location', locationCallback);
}
function startLocationMonitor() {
Ti.Geolocation.addEventListener('location', locationCallback);
}