Get latitude/longitude and stop location runtime event

Hi, I am developing a weather app that gets your current location and display the weather details. The problem at the moment is that when i test the app out on the device, the event.latitude and event.longitude keeps updating too.

I just want to start the app, get the positions once and stop the runtime location event.

Any thoughts?

Hi @contact551,

If you post some of your code, it will help people diagnose a potential solution. As a quick initial consideration, I think you’d just add the Runtime listener, check when you receive both a latitude and longitude, then remove the Runtime listener and process those values that you received.

Take care,

Brent

I noticed this and also noticed somebody highlighting it in the forums a number of years ago - I found that the position of my listener function helped this - upon certain conditions the Runtime:removeEventListener(“location”, listener) just didn’t have any effect.

I’ll dig out my code later and try to help out.

Hi Brett, this is my code:

local lat = nil local lon = nil local locationText = display.newText("", display.contentCenterX, display.contentCenterY, nil, 18 ) local function myListener(event) if event.errorCode then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) print( "Location error: " .. tostring( event.errorMessage ) ) else lat = event.latitude lon = event.longitude if lat ~= nil and lon ~= nil then locationText.text = lat.." "..lon Runtime:removeEventListener( "location", myListener ) end end end Runtime:addEventListener( "location", myListener )

It seems the Runtime:removeEventListener has no effect in here. The vars lat and long keep changing as I walk with the device, but all I want to do is to register the user location when the app opens and then stop. Any ideas?

Hi @contact551,

If you post some of your code, it will help people diagnose a potential solution. As a quick initial consideration, I think you’d just add the Runtime listener, check when you receive both a latitude and longitude, then remove the Runtime listener and process those values that you received.

Take care,

Brent

I noticed this and also noticed somebody highlighting it in the forums a number of years ago - I found that the position of my listener function helped this - upon certain conditions the Runtime:removeEventListener(“location”, listener) just didn’t have any effect.

I’ll dig out my code later and try to help out.

Hi Brett, this is my code:

local lat = nil local lon = nil local locationText = display.newText("", display.contentCenterX, display.contentCenterY, nil, 18 ) local function myListener(event) if event.errorCode then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) print( "Location error: " .. tostring( event.errorMessage ) ) else lat = event.latitude lon = event.longitude if lat ~= nil and lon ~= nil then locationText.text = lat.." "..lon Runtime:removeEventListener( "location", myListener ) end end end Runtime:addEventListener( "location", myListener )

It seems the Runtime:removeEventListener has no effect in here. The vars lat and long keep changing as I walk with the device, but all I want to do is to register the user location when the app opens and then stop. Any ideas?

Same problem here, can’t seem to be able to get lat and lng only once.

I would love to get an answer to this as well. Currently this issue causes unnecessary battery drain. There must be a way around this. Thanks for your help.

It is because you can’t remove a listener from inside a listener. 

try to put your removeListener call in a timer.performWithDelay closure to see if it helps.

 timer.performWidthDelay( 10, -- 10 ms delay, just to "get out" of the listener scope function(e) Runtime:removeEventListener("location", myListener) end )
local updateLocation = function() local function locationHandler(event) if event.errorCode then print(" ERROR : " .. tostring( event.errorMessage ) ) else print(" LAT : "..event.latitude..", LONG : "..event.longitude) end Runtime:removeEventListener( "location", locationHandler ) end Runtime:addEventListener( "location", locationHandler ) end

Can confirm this is still a bug on all device. The removeEventListener isn’t firing off, other than on the Corona Simulator.

We would need an simple app that shows this problem.  We need a complete sample app (main.lua, your build.settings and your config.lua that’s causing the issue) in a zip file.  Click on the “Report a Bug” link at the top of the page.  When you get the email confirmation, please post the bug # back here as a reference.

Once we see the code, we can help try and figure this out or get it to engineering if it’s a bug.

Rob

Sent in a zip a few days ago and still no email confirmation.

Hi @zv713,

Did you submit this through the official bug/issue report system, or via e-mail?

Brent

Though the official system found in the Report a Bug link up top.

Thank you. You may not receive a prompt e-mail, as we have to prioritize this issue and test it on our side… but it’s logged in our tracking system now.

Brent

I’ve dug through the bug tracker and I don’t see any thing when searching for keywords from this post.  Can you try and re-submit it?  You should get an automated message (not from a human) with the case # in it.  Please check your spam posts to see if it when there.

Rob