Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Location works strangely the second time
Started by gordon1bob May 03 2016 08:18 AM

10 replies to this topic
gps addeventlistener removeeventlistener
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

gordon1bob

[GLOBAL: userInfoPane.html]
gordon1bob
  • Enthusiast

  • 41 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Corona Geek

  • 1,077 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

gordon1bob

[GLOBAL: userInfoPane.html]
gordon1bob
  • Enthusiast

  • 41 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

gordon1bob

[GLOBAL: userInfoPane.html]
gordon1bob
  • Enthusiast

  • 41 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,558 posts
  • Enterprise

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



[TOPIC: post.html]
#6

gordon1bob

[GLOBAL: userInfoPane.html]
gordon1bob
  • Enthusiast

  • 41 posts
  • Corona SDK

Rob,

 

Test project is under this topic: 

Location Handler Appears to Stop

[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,558 posts
  • Enterprise

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



[TOPIC: post.html]
#8

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,558 posts
  • Enterprise

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



[TOPIC: post.html]
#9

gordon1bob

[GLOBAL: userInfoPane.html]
gordon1bob
  • Enthusiast

  • 41 posts
  • Corona SDK

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

Attached Files



[TOPIC: post.html]
#10

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,558 posts
  • Enterprise

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.



[TOPIC: post.html]
#11

gordon1bob

[GLOBAL: userInfoPane.html]
gordon1bob
  • Enthusiast

  • 41 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]