Then user taps same button. LocationHandler is invoked immediately as before. Some minutes later things happen. When they happen, they do the right stuff, but a five-ten minute delay?
I don’t have much familiarity with this, but why not just keep the event listener active? Perhaps use a switch in locationHandler so the code only executes when necessary.
It takes time after starting up the location handler functions to acquire the GPS satellites. Five minutes seems kinda long though. It should fire up in 10-30 seconds worse case. Today, Apple gives you the option to only have the GPS running in the foreground, so it’s better on the battery than when running in the background.
Good morning. I have modified the example program. There is now a button “Timed Location”. Tap it. Every 5 seconds it turns on the location handler, which turns off after a second. After 2 - 4 times, the handler stops responding.
I’ve not looked at your modified one, but the one you sent yesterday stopped working in the sim after a few presses of the button. I made the following changes and now it doesn’t stop working:
local removeHandler -- forward declare for the remove function. You need to know about it -- before you define it. local locationHandler = function( event ) log.trace("locationHandler") if ( event.errorCode ) then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) else if delegate then removeHandler() -- call the remove handler here after you've handle the event -- this might even be safer dropped into a short timer: -- timer.performWithDelay(10, removeHandler, 1 ) delegate(event) end end end removeHandler = function() -- since this was forward declared, you have to use the anonymous function syntax log.trace("removeHandler") Runtime:removeEventListener( "location", locationHandler ) end local function getLocation() Runtime:addEventListener("location",locationHandler) --timer.performWithDelay(1000,removeHandler) -- take it off of the timer end
I also came up with a solution: I used a mapview. Since the project does not require the user to actually use the map, I placed a small map off the screen and called getUserLocation. When I get the location I call removeSelf on the map.
I’ll try your suggestion, too. I’m still doing lots of learning.
I don’t have much familiarity with this, but why not just keep the event listener active? Perhaps use a switch in locationHandler so the code only executes when necessary.
It takes time after starting up the location handler functions to acquire the GPS satellites. Five minutes seems kinda long though. It should fire up in 10-30 seconds worse case. Today, Apple gives you the option to only have the GPS running in the foreground, so it’s better on the battery than when running in the background.
Good morning. I have modified the example program. There is now a button “Timed Location”. Tap it. Every 5 seconds it turns on the location handler, which turns off after a second. After 2 - 4 times, the handler stops responding.
I’ve not looked at your modified one, but the one you sent yesterday stopped working in the sim after a few presses of the button. I made the following changes and now it doesn’t stop working:
local removeHandler -- forward declare for the remove function. You need to know about it -- before you define it. local locationHandler = function( event ) log.trace("locationHandler") if ( event.errorCode ) then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) else if delegate then removeHandler() -- call the remove handler here after you've handle the event -- this might even be safer dropped into a short timer: -- timer.performWithDelay(10, removeHandler, 1 ) delegate(event) end end end removeHandler = function() -- since this was forward declared, you have to use the anonymous function syntax log.trace("removeHandler") Runtime:removeEventListener( "location", locationHandler ) end local function getLocation() Runtime:addEventListener("location",locationHandler) --timer.performWithDelay(1000,removeHandler) -- take it off of the timer end