Page 1 of 3

how to call Geolocation Service every few seconds

Posted: Wed Jul 03, 2013 4:09 pm
by ciccio

i have an app with a geolocation service.
i map data from the geolocation service to some local storage variables
and i need to invoke the geolocation service every 30 sec and send obtained data to another service.
i tried to add an asset js
code function run_geolocation() {
geolocation.execute({});
setTimeout("run_geolocation()", 15000);

}
/code

then on page load i added coderun_geolocation()/code

and on geolocation success event i added
codevar latitude = localStorage.getItem('latitude');
var longitude = localStorage.getItem('longitude');
var altitude = localStorage.getItem('altitude');
var speed = localStorage.getItem('speed');
var accuracy = localStorage.getItem('accuracy');
var uuid = localStorage.getItem('uuid');
var time = localStorage.getItem('time');
var cdati = 'APPERY'+','+latitude+','+longitude+','+altitude+','+speed+','+accuracy+','+uuid;

alert(time + '-' + cdati);
localStorage.setItem('cdati', cdati);
//invia.execute({});/code

the problem is that the first time i load the page i get 1 alert, but when the geolocation service is executed after 15 sec i get 2 alerts, after more 15 sec i get 3 alerts and so on

what is wrong with my code??
i would like to have just 1 alert (actually /invia.execute({});) each 15 sec

could you help me to understand if i need to use some different way to call every 15 sec the geolocation service?

i took a look to
https://getsatisfaction.com/apperyio/...
https://getsatisfaction.com/apperyio/...

but i didn't understand how to solve my problem


how to call Geolocation Service every few seconds

Posted: Wed Jul 03, 2013 4:48 pm
by Kateryna Grynko

Hi.

Replace your code:

codefunction run_geolocation() {
geolocation.execute({});
setTimeout("run_geolocation()", 15000);
} /code
With the following:precodefunction run_geolocation(interval_in_seconds) {
var t;
interval_in_seconds = interval_in_seconds1000 || 151000;
return {
stop: function(){
clearInterval(t);
},
start: function(){
t = setInterval("console.log(new Date())", interval_in_seconds);
}
}
}/code/pre
To run:
codetimer = create_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
timer.start();/code
To stop:
codetimer.stop();/code
You should be careful. If you call two timer.start(); sequentially then you won't be able to stop the first timer because a pointer to it will be removed.


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 8:54 am
by ciccio

sorry, but where i should call the geolocation service?

should i insert in your code geolocation.execute({}); and if yes where?
or should i first invoke the service on load event and then run the JS
codetimer = create_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
timer.start();/code?

i tried to replace my code with the suggested one but nothing happens


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 9:46 am
by Maryna Brodina

Hi, sorry - there're some mistakes in Katya's code. Instead of this code codetimer = create_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
timer.start();/code should be codetimer = run_geolocation(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
timer.start();/code

Also here:
codefunction run_geolocation(interval_in_seconds) {
var t;
interval_in_seconds = interval_in_seconds1000 || 151000;
return {
stop: function(){
clearInterval(t);
},
start: function(){
t = setInterval("console.log(new Date())", interval_in_seconds);
}
}
}/code

instead of codeconsole.log(new Date())/code should be codegeolocation.execute({})/code

So the final code is:
codefunction run_geolocation(interval_in_seconds) {
var t;
interval_in_seconds = interval_in_seconds1000 || 151000;
return {
stop: function(){
clearInterval(t);
},
start: function(){
t = setInterval("geolocation.execute({})", interval_in_seconds);
}
}
}/code


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 9:49 am
by ciccio

I'm trying to work on the js i replaced
codefunction run_geolocation(interval_in_seconds) {
var t;
interval_in_seconds = interval_in_seconds1000 || 151000;
return {
stop: function(){
clearInterval(t);
},
start: function(){
t = setInterval("console.log(new Date())", interval_in_seconds);
}
}
}/code

by trying to add geolocation.execute({}); at the beginning of the function or after t = setInterval("console.log(new Date())", interval_in_seconds); but i'm not able to run the geolocation service evere 30 sec

please could you help me?


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 10:02 am
by ciccio

thanks,
i tested your code but if to run i use
code timer = run_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
timer.start();/code
i get from console a js error : "Uncaught ReferenceError: run_geolocation_timer is not defined "

so i changed to timer = run_geolocation(30);

but even if on geolocation service success event i have the following code codevar latitude = localStorage.getItem('latitude');
var longitude = localStorage.getItem('longitude');
var altitude = localStorage.getItem('altitude');
var speed = localStorage.getItem('speed');
var accuracy = localStorage.getItem('accuracy');
var uuid = localStorage.getItem('uuid');
var time = localStorage.getItem('time');
var cdati = 'APPERY'+','+latitude+','+longitude+','+altitude+','+speed+','+accuracy+','+uuid;

alert(time + '-' + cdati);/code

when i test the app on my browser i get 1 alert the first time the geolocation service has been invoked but after 30 sec whe there is the second invocation i don't get any alert and i can just see the spinner running for few seconds


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 10:10 am
by ciccio

actually i added two alerts on geolocation service fail and complete events and the second time the geolocation service has invoked i get the fail alert.


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 10:22 am
by ciccio

i guess the prolem is with the geolocation service settings.
on my desktop browser:

if i check watch position true i get the fail alert since the second invocation
if watch position is unchecked everything is ok

on my mobile (using appery tester):
i always get the fail aler (even if watch position is unchecked)


how to call Geolocation Service every few seconds

Posted: Thu Jul 04, 2013 10:56 am
by ciccio

thanks it is working


how to call Geolocation Service every few seconds

Posted: Wed Jul 10, 2013 10:48 am
by ciccio

hello,
i modified my project by checking (true) the watch position request property of my geolocation service.
and even if im following all your suggestion:

I have a JS asset with:
codefunction run_geolocation(interval_in_seconds) {

Code: Select all

 var t; 

interval_in_seconds = interval_in_seconds1000 || 151000;
return {
stop: function(){
clearInterval(t);
},
start: function(){
t = setInterval("geolocation.execute({})", interval_in_seconds);
}
}
}/code
and on page load i run a js:

code
timer = run_geolocation(10); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
timer.start();
/code

on geolocation success event i have code
var latitude = localStorage.getItem('latitude');
var longitude = localStorage.getItem('longitude');
var altitude = localStorage.getItem('altitude');
var speed = localStorage.getItem('speed');
var accuracy = localStorage.getItem('accuracy');
var uuid = localStorage.getItem('uuid');
var time = localStorage.getItem('time');
var cdati = 'APPERY'+','+latitude+','+longitude+','+altitude+','+speed+','+accuracy+','+uuid;

//alert(cdati);
localStorage.setItem('cdati', cdati);
alert(localStorage.getItem('cdati'));

//invia.execute({});/code

and on device ready event i have codevar string = device.uuid;
localStorage.setItem('uuid', string);/code

the first time i load the page i get 1 alert, but when the geolocation service is executed after 15 sec i get 2 alerts, after more 15 sec i get 3 alerts and so on
as you can see from the following link
http://appery.io/app/mobile-frame?src...

P.S.: if the watch position request property is not checked everything is ok

what is the problem according to you?