how to call Geolocation Service every few seconds

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
function run_geolocation() {
geolocation.execute({});
setTimeout("run_geolocation()", 15000);

}


then on page load i added run_geolocation()

and on geolocation success event i added
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(time + '-' + cdati);
localStorage.setItem('cdati', cdati);
//invia.execute({});


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
1 person has
this question
+1
Reply
  • Hi.

    Replace your code:

    function run_geolocation() {
    geolocation.execute({});
    setTimeout("run_geolocation()", 15000);
    }

    With the following:
    function run_geolocation(interval_in_seconds) {
    var t;
    interval_in_seconds = interval_in_seconds*1000 || 15*1000;
    return {
    stop: function(){
    clearInterval(t);
    },
    start: function(){
    t = setInterval("console.log(new Date())", interval_in_seconds);
    }
    }
    }

    To run:
    timer = create_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
    timer.start();

    To stop:
    timer.stop();
    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.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • 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
    timer = create_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
    timer.start();
    ?

    i tried to replace my code with the suggested one but nothing happens
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

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


    Also here:
    function run_geolocation(interval_in_seconds) {
    var t;
    interval_in_seconds = interval_in_seconds*1000 || 15*1000;
    return {
    stop: function(){
    clearInterval(t);
    },
    start: function(){
    t = setInterval("console.log(new Date())", interval_in_seconds);
    }
    }
    }


    instead of console.log(new Date()) should be geolocation.execute({})

    So the final code is:
    function run_geolocation(interval_in_seconds) {
    var t;
    interval_in_seconds = interval_in_seconds*1000 || 15*1000;
    return {
    stop: function(){
    clearInterval(t);
    },
    start: function(){
    t = setInterval("geolocation.execute({})", interval_in_seconds);
    }
    }
    }
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • This reply was removed on 2013-07-04.
    see the change log
  • thanks,
    i tested your code but if to run i use
    timer = run_geolocation_timer(30); // interval 30 seconds, calling without arguments will set interval to 15 seconds.
    timer.start();

    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 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(time + '-' + cdati);


    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
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • 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.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • 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)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • 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:
    function run_geolocation(interval_in_seconds) {

    var t;
    interval_in_seconds = interval_in_seconds*1000 || 15*1000;
    return {
    stop: function(){
    clearInterval(t);
    },
    start: function(){
    t = setInterval("geolocation.execute({})", interval_in_seconds);
    }
    }
    }

    and on page load i run a js:


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


    on geolocation success event i have
    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({});


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


    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?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hello,

    Sorry, we cannot reproduce the problem, the code is executed correctly.
    Please tell us how to reproduce the issue.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • hello,
    i'm sorry for the late answer,
    when the watch position request property of my geolocation service is checked (true):
    1) from the PC desktop the first time the service is run i get the alert i added to the geolocation service success event. From the second time the servis is rune i get the alert i added to the geolocation service fail event;
    2) from the appery tester on my mobile (samsung galaxy S2 android 4.03) The first time i load the page i get 1 success 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

    P.S:: the public link to test the application is: http://appery.io/app/mobile-frame?src....

    if you need i can share the application with support@appery.io
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • I can't get the watch position setting to change? Is this an Appery.io setting?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hello Lee,

    Sorry, I'm not sure I understand you correctly. Could you please detail your issue?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • When I am learning about geolocation service. I would like to test the "watch" service.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hi Lee,

    You can change this settings in service datasource.

    See details: http://prntscr.com/5pf2i4/direct

    Regards.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hi,
    I am using the Geolocation Service on my app. My app gets the co-ordinates and then sends it to my database.
    I have few questions.
    1. Is there any limit on the number of calls I can make per day to get geolocation?
    2. I see a pattern where I don't see location data on highways but reasonable data inside urban or city areas. Does that have to do with number of cellphone towers my phone can hit? I have unchecked fine grained location but still do not see location data when I travel through highways. But I am able to send push notifications and it reaches instantaneously even though my app isn't reporting any location data.
    3. Would you recommend com.google.android.gms.location over android.location? Is there any sample for com.google.android.gms.location?
    Thanks
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hi Tarun,



    1. Is there any limit on the number of calls I can make per day to get geolocation?



    Nope device geolocation service have not these limitations.

    2 - you need to set "enableHighAccuracy" option in the "before send" service mapping.
    Details: http://prntscr.com/6zre41/direct

    3. It's up to you what to use in your app. Appery.io based on Cordova/phonegap. There is a lot of plugins for this system. Also you can write your own.

    Regards.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • Hi

    The code works perfectly but the IDE reports lots of errors when I cut/paste your code to to the js file.
    Is the code depreciated or outdated? Do you have a new solution for this?

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly happy, confident, thankful, excited sad, anxious, confused, frustrated indifferent, undecided, unconcerned

  • This reply was removed on 2016-06-30.
    see the change log