Create a gist now

Instantly share code, notes, and snippets.

Embed
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()
@raremedia

This comment has been minimized.

Show comment
Hide comment
@raremedia

raremedia 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.

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