GPS loop

Found this code via CODE EXCHANGE and it should do what i need. But for some reason it stopsafter a few runs. My skills are lacking so i cant figure why it stops. I need to get GPS position x seconds until i stop the app . Can anyone tell me why it stops ??

local currentLatitude = 0 local currentLongitude = 0 local updateGps = 5000 -- update Gps every X seconds local locationHandler = function( event ) -- On update, stop listening to GPS signal to avoid battery draining Runtime:removeEventListener( "location", locationHandler ) -- Check for error (user may have turned off Location Services) if event.errorCode then print( "Location error: " .. tostring( event.errorMessage ) ) else currentLatitude = string.format( '%.4f', event.latitude ) currentLongitude = string.format( '%.4f', event.longitude ) print('current latitude: ' .. currentLatitude) print('current longitude: ' .. currentLongitude) end end local function updateGPSTimer() -- Reload GPS location Runtime:addEventListener( "location", locationHandler ) -- Update again timer.performWithDelay( 5000, updateGPSTimer ) end -- Start GPS timer updateGPSTimer()

You need to add iterations (how many times it runs) to your timer. 

timer.performWithDelay( 5000, updateGPSTimer, -1)

This should make it run infinite times.

Actually that code is pretty bad. Every 5 seconds it tries to re-add the GPS listener which isn’t doing what one might thing.

You should only start the location services listener once. GPS events appears to be based on movement (since both the accuracy and threshold settings are distance based). If you’re not moving you may not get events. So you should probably save your GPS information when you do get a GPS event and then have a timer every 5 seconds (or how often) that save the last recorded GPS data.

Rob

Actually i found the GPS sample included with Corona sdk instead exactly what i needed. My only concern is if the GPS event is fired every time i move wont that drain the battery ? even if i as you say Rob save the location every 5 seconds. 

GPS is a drain on the battery. But it should only be generating events when you’re in the foreground. Many apps use the GPS. On my iPhone the location Icon is almost always on. Devices have gotten better about their energy usage and while as a developer you should be aware of the battery drain, you shouldn’t go to extreme measures. Restarting location services every 5 seconds would be a harder drain than just letting it run since it has to do a lot of work to reacquire the GPS satellites.

There are two API calls, both distance based:

https://docs.coronalabs.com/api/library/system/setLocationAccuracy.html

https://docs.coronalabs.com/api/library/system/setLocationThreshold.html

Accuracy is how pin-point you want the coordinates to be. To be very accurate, it has to talk to more satellites and more frequently. Threshold controls how often events are generated which can help with your energy consumption. If you’re sitting still you likely won’t get very frequent updates. But if you’re in a moving car, you will get more frequent updates. Setting this value will let you somewhat control that. 10 meters would not generate many events while moving around your house. But traveling at freeway speeds would generate a considerable number of events.

Rob

You need to add iterations (how many times it runs) to your timer. 

timer.performWithDelay( 5000, updateGPSTimer, -1)

This should make it run infinite times.

Actually that code is pretty bad. Every 5 seconds it tries to re-add the GPS listener which isn’t doing what one might thing.

You should only start the location services listener once. GPS events appears to be based on movement (since both the accuracy and threshold settings are distance based). If you’re not moving you may not get events. So you should probably save your GPS information when you do get a GPS event and then have a timer every 5 seconds (or how often) that save the last recorded GPS data.

Rob

Actually i found the GPS sample included with Corona sdk instead exactly what i needed. My only concern is if the GPS event is fired every time i move wont that drain the battery ? even if i as you say Rob save the location every 5 seconds. 

GPS is a drain on the battery. But it should only be generating events when you’re in the foreground. Many apps use the GPS. On my iPhone the location Icon is almost always on. Devices have gotten better about their energy usage and while as a developer you should be aware of the battery drain, you shouldn’t go to extreme measures. Restarting location services every 5 seconds would be a harder drain than just letting it run since it has to do a lot of work to reacquire the GPS satellites.

There are two API calls, both distance based:

https://docs.coronalabs.com/api/library/system/setLocationAccuracy.html

https://docs.coronalabs.com/api/library/system/setLocationThreshold.html

Accuracy is how pin-point you want the coordinates to be. To be very accurate, it has to talk to more satellites and more frequently. Threshold controls how often events are generated which can help with your energy consumption. If you’re sitting still you likely won’t get very frequent updates. But if you’re in a moving car, you will get more frequent updates. Setting this value will let you somewhat control that. 10 meters would not generate many events while moving around your house. But traveling at freeway speeds would generate a considerable number of events.

Rob