Refresh the GPS location every 2 secs

You must Login before you can answer or comment on any questions.

Hi! I have a problem with my app, I calculate the with the GPS the location and I want to do this every 2secs. This is my code:

setInterval(Titanium.Geolocation.getCurrentPosition(function(e){
        timerStarted = true;
        if(e.error)
        {
            alert('The location is not available');
            return;
        }
        var longitude = e.coords.longitude;
        var latitude = e.coords.latitude;
        var timestamp = e.coords.timestamp;
        currentLocation.text = 'longitude:' + longitude + ' latitude: ' + latitude;
        Titanium.API.info('geo - current heading: ' + new Date(timestamp) + ' x ' + x + ' y ' + y + ' z ' + z);
    }),2000);
After 2 seconds it stops completely and also the simulation goes down

2 Answers

Accepted Answer

First problem is that you're not giving a function reference to setInterval(), you're actually executing code and passing the return value to setInterval().

Note that the call to getCurrentPosition() is asynchronous -- you don't know how long it might take to run. You can't assume it will be done in 2 seconds. I don't know exactly what happens when you make a call to getCurrentPosition() while a previous one is active, but I don't think it would be good.

You could continue down this path, fixing your function reference issue and use setTimeout() instead:

function getLocation () {
    Titanium.Geolocation.getCurrentPosition(function(e){
        timerStarted = true;
        if(e.error)
        {
            alert('The location is not available');
            return;
        }
        var longitude = e.coords.longitude;
        var latitude = e.coords.latitude;
        var timestamp = e.coords.timestamp;
        currentLocation.text = 'longitude:' + longitude + ' latitude: ' + latitude;
        Titanium.API.info('geo - current heading: ' + new Date(timestamp) + ' x ' + x + ' y ' + y + ' z ' + z);
 
        setTimeout (getLocation, 2000);
    });
}
 
setTimeout (getLocation, 2000);
This way, the system waits until the location is obtained before starting another 2-second interval.

Note that this is probably too aggressive and will probably have battery life implications. You probably also want to consider using the techniques for continually monitoring GPS position per the guides.

Try this:

var cron = setInterval(function(){
    Ti.Geolocation.getCurrentPosition(function(e) {
        // do something 
    });
}, 2000);
 
// stopping after 10 min:
setTimeout(function(){clearInterval(cron);},10*60*1000);
!!! This code eats your battery power!

— answered 11 months ago by Rainer Schleevoigt
answer permalink
1 Comment
  • i know but for know it's ok..i only started yesterday! it works, but the position is always the same, i think its because the getCurrentPosition method gives only the cached position.

    — commented 11 months ago by Cristina Nardin

Your Answer

Think you can help? Login to answer this question!