Ok, I took a few minutes to write a test program.
There is a text object showing the current location latitude. To make it easier to see the event firing, without checking the console, I made the text object change color every time the location event fires.
With the Corona sim, the event fires only ONCE, no matter if the removeEventListener is called or not.
With the iOS sim, or the actual device, on the other hand, the events KEEP firing repeatedly regardless of removeEventListener.
[code]
local txt = display.newText("Lat: ",0,0,native.systemFont, 18)
txt.x,txt.y = display.contentWidth/2,display.contentHeight/2;
local locationHandler = function( event )
– Check for error (user may have turned off Location Services)
if event.errorCode then
native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )
print( "Location error: " … tostring( event.errorMessage ) )
else
print("Lat: ",event.latitude);
local color = {};
color[1] = math.random(255)
color[2] = math.random(255)
color[3] = math.random(255)
txt.text = "Lat: "…event.latitude;
txt:setTextColor(color[1],color[2],color[3]);
–I want the event to fire only once, then remove the event listener
–testing on the iOS simulator and the actual iPhone device, the event never gets removed
print(“Removing the event listener so it fires only once”);
Runtime:removeEventListener( “location”, locationHandler );
end
end
– Activate location listener
Runtime:addEventListener( “location”, locationHandler );
[/code] [import]uid: 33608 topic_id: 22037 reply_id: 94862[/import]