forked from benbahrenburg/Ti.Geo.Background
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
112 lines (91 loc) · 2.59 KB
/
app.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
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
/*jslint maxerr:1000 */
var my = {
bGeo : require('bGeo/Ti.Geo.Background'),
isAndroid : Ti.Platform.osname === 'android',
session:{started:false}
};
my.bGeo.purpose = "Demo Background Rocks";
my.bGeo.distanceFilter = 100; //100 meters
my.bGeo.trackSignificantLocationChange = true;
my.bGeo.minAge = 3;
my.bGeo.maxAge = 30;
(function () {
var win = Ti.UI.createWindow({
backgroundColor: '#fff', title: 'Background Geo Rocks',
barColor:'#000',fullscreen:false
});
var mapView = Ti.Map.createView({
top:80, bottom:0, left:5, right:5, userLocation:false
});
win.add(mapView);
var startStopButton = Ti.UI.createButton({
title:((my.session.started) ? 'stop' :'start'),
top:10, height:40,left:5, width:75
});
win.add(startStopButton);
var clearButton = Ti.UI.createButton({
title:'Clear', top:10, height:40,left:85, width:75
});
win.add(clearButton);
var refreshButton = Ti.UI.createButton({
title:'Refresh', top:10, height:40,left:165, width:75
});
win.add(refreshButton);
clearButton.addEventListener('click',function(e){
my.bGeo.clearCache();
mapView.removeAllAnnotations();
});
startStopButton.addEventListener('click',function(e){
if(my.session.started){
my.bGeo.stop();
my.session.started = false;
}else{
my.bGeo.start();
my.session.started = true;
}
startStopButton.title=((my.session.started) ? 'stop' :'start');
});
refreshButton.addEventListener('click',function(e){
mapView.removeAllAnnotations();
var results = my.bGeo.readCache();
for (iLoop=0;iLoop < results.length;iLoop++){
assistant.addToMap(results[iLoop]);
}
});
var assistant = {
addToMap : function(e){
var pin = Ti.Map.createAnnotation({
latitude:e.latitude,longitude:e.longitude
});
mapView.addAnnotation(pin);
var region = {latitude:e.latitude,longitude:e.longitude,
latitudeDelta:0.04, longitudeDelta:0.04};
mapView.setLocation(region);
},
locationChangeCallback : function(e){
Ti.API.info('Location changed');
assistant.addToMap(e);
},
locationError : function(e){
alert('Error due to ' + e.message);
}
};
my.bGeo.addEventListener('change',assistant.locationChangeCallback);
my.bGeo.addEventListener('error',assistant.locationError);
win.open({modal:true});
})();
if(!my.isAndroid){
Ti.App.addEventListener('resumed',function(e){
if(my.session.started){
my.bGeo.stop();
}
});
Ti.App.addEventListener('close',function(e){
my.bGeo.stop();
});
Ti.App.addEventListener('pause',function(e){
if(my.session.started){
my.bGeo.start();
}
});
}