Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 ) )
currentLatitude = string.format( '%.4f', event.latitude )
currentLongitude = string.format( '%.4f', event.longitude )
print('current latitude: ' .. currentLatitude)
print('current longitude: ' .. currentLongitude)
local function updateGPSTimer()
-- Reload GPS location
Runtime:addEventListener( "location", locationHandler )
-- Update again
timer.performWithDelay( 5000, updateGPSTimer )
-- Start GPS timer

This comment has been minimized.

Copy link

commented Dec 17, 2014

Just a note: The Runtime:removeEventListener() call will not execute because you cannot remove a listener inside it's own listener function. This snippet just resets the location listener every 5 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.