Location works strangely the second time

Good morning.

User taps a button.

I install an event listener to figure out where (geographically) I am:

Runtime:addEventListener(“location”,locationHandler)

LocationHandler() gets called correctly. I figure out where I am and do some other stuff.

Then, I remove the listener:

Runtime:removeEventListener(“location”,locationHandler)

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?

Off to give blood now.

Thank you.

-Bob Gordon

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.

I have posted an simple project that demonstrates the issue in “Location Handler Appears to Stop.” Please look there.

The reason I want to turn the handler off is that continually refreshing the location is a battery drain.

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.

I didn’t see a link to your test project.

Rob

Rob,

Test project is under this topic: 

Location Handler Appears to Stop

We prefer you to only have one thread open for discussing an issue.

The thread with the downloadable link for anyone wanting to look at this is:

https://forums.coronalabs.com/topic/63076-location-handler-appears-to-stop/

I will be locking that thread, please continue the conversation here.

Rob

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.

-Bob

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

Give that a try.

Rob, 

Thank you.

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.

-Bob Gordon

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.

I have posted an simple project that demonstrates the issue in “Location Handler Appears to Stop.” Please look there.

The reason I want to turn the handler off is that continually refreshing the location is a battery drain.

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.

I didn’t see a link to your test project.

Rob

Rob,

Test project is under this topic: 

Location Handler Appears to Stop

We prefer you to only have one thread open for discussing an issue.

The thread with the downloadable link for anyone wanting to look at this is:

https://forums.coronalabs.com/topic/63076-location-handler-appears-to-stop/

I will be locking that thread, please continue the conversation here.

Rob

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.

-Bob

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

Give that a try.