Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

getting the event.location value into a variable when calling a location handler function
Started by marty.lindsay Mar 07 2016 06:51 PM

3 replies to this topic
gps location addeventlistener function local global
[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

marty.lindsay

[GLOBAL: userInfoPane.html]
marty.lindsay
  • Observer

  • 6 posts
  • Corona SDK

Hi, I'm hoping for your experience to fix my error.

What I want to achieve is this:

At phone start, phone locates lattitude and longitude, these then get concatenated into a variable, which is then used later in the code to construct a url.

 

I am so close but have hit a wall.  A code snippet below:

 

local latlongdisplay = display.newText( "-", 100, 50, native.systemFont, 16 )


locationHandler = function( event )
if ( event.errorCode ) then
native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )
print( "Location error: " .. tostring( event.errorMessage ) )
else
local latlongtext = string.format( '%.4f', event.latitude ) .. "," .. string.format( '%.4f', event.longitude )
latlongdisplay.text = latlongtext
end
return latlongtext
end


Runtime:addEventListener( "location", locationHandler )


if latlongtext == nil then
print( " it was nil" )
else 
print( "it was: " .. latlongtext )
end

so.. this works fine to populate the display.newText object, and I see it on screen... however, the actual local variable of latlongtext remains nil.

 

Can someone please advise how I reliably get the value of latlongtext OUT of the function, so I can use it elsewhere.  

ps, I know it's a local variable inside the if/then clause, but I'd prefer to not have to use a global variable, when there is likely a better method.

 

thanks all



[TOPIC: post.html]
#2

Alex@Panc

[GLOBAL: userInfoPane.html]
Alex@Panc
  • Corona Geek

  • 1,729 posts
  • Corona SDK

You've got a lot going on here, but the basic way you would resolve this, is to scope your latlongtext variable higher in your module/scene, update the value and don't create it in the Runtime listener, and move your print() call to be within your Runtime listener. Something like this:

local latlongdisplay = display.newText( "-", 100, 50, native.systemFont, 16 )
local latlongtext

locationHandler = function( event )
	if ( event.errorCode ) then
		native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} )
		print( "Location error: " .. tostring( event.errorMessage ) )
	else
		latlongtext = string.format( '%.4f', event.latitude ) .. "," .. string.format( '%.4f', event.longitude )
		latlongdisplay.text = latlongtext
	end
	if latlongtext == nil then
		print( " it was nil" )
	else 
		print( "it was: " .. latlongtext )
	end
end


Runtime:addEventListener( "location", locationHandler )


Untested so YMMV.



[TOPIC: post.html]
#3

marty.lindsay

[GLOBAL: userInfoPane.html]
marty.lindsay
  • Observer

  • 6 posts
  • Corona SDK

thanks very much, that makes sense, I'll do more testing.



[TOPIC: post.html]
#4

marty.lindsay

[GLOBAL: userInfoPane.html]
marty.lindsay
  • Observer

  • 6 posts
  • Corona SDK

next question. I want to use the value for latlongtext further in my code, but no matter what I try , it is always nil.

i.e After I call this line 

Runtime:addEventListener( "location", locationHandler )

 

I want to use the variable latlongtext in the construction of a url. It appears to me that the return value is passed back to the caller of the locationHandler function, which in this case is the Runtime:addEventListener

 

so.. can you advise the best way to get the lat and long INTO variable that I can use elsewhere?




[topic_controls]
[/topic_controls]