Location events

Hello everyone,

I’m trying to get GPS verifycation on a loaction. Though, when i try to get the user loaction, it gives me nil.

To bring everything back to basic, I used this code:

local lat = event.latitude local long = event.longitude print( lat ) print ( long )

When I look at the console, it prints nil. Anyone got any good ideas?

Hi @rikben,

Can you please post more of your code, including where you set up the listener function? We (or other developers) will need more info to assist you on this.

Thanks,

Brent

Hi,

Thanks for getting back to me. I’ll post my code. I copied some content from the docs. The permissions are set correctly.

----------------------------------------------------------------------------------------- -- -- scene2.lua -- ----------------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local function returnHome() storyboard.gotoScene( "splash", "crossFade", 1000 ) return true end function scene:createScene( event ) local group = self.view local background = display.newImage("workscreen.png") background.x = display.contentWidth/2 background.y = 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 local latitudeText = string.format( '%.4f', event.latitude ) currentLatitude = latitudeText latitude:setText( latitudeText ) local longitudeText = string.format( '%.4f', event.longitude ) currentLongitude = longitudeText longitude:setText( longitudeText ) local altitudeText = string.format( '%.3f', event.altitude ) altitude:setText( altitudeText ) local accuracyText = string.format( '%.3f', event.accuracy ) accuracy:setText( accuracyText ) local speedText = string.format( '%.3f', event.speed ) speed:setText( speedText ) local directionText = string.format( '%.3f', event.direction ) direction:setText( directionText ) -- Note that 'event.time' is a Unix-style timestamp, expressed in seconds since Jan. 1, 1970 local timeText = string.format( '%.0f', event.time ) time:setText( timeText ) end end -- Activate location listener Runtime:addEventListener( "location", locationHandler ) local lat = event.latitude local long = event.longitude local myText = display.newText( "Lat: " .. lat, 100, 200, native.systemFont, 16 ) myText:setFillColor( 0, 0, 0 ) print( lat ) print ( long ) appicon = widget.newButton{ defaultFile="code\_but.png", onRelease = returnHome } appicon.x = 1015 appicon.y = 643 group:insert ( background ) group:insert ( appicon ) end function scene:enterScene( event ) local group = self.view end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view if appicon then appicon:removeSelf() appicon = nil end end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Thanks,

Rik

Are you trying to run this in the simulator?  Are you building for a device?  What device?  If you’re doing android do you have all the right permissions in your build.settings?

Can you build and run the GPS sample app?

Rob

Hi,

I have tried to run it in the simulator. That doesn’t work. I am developping for a Samsung galaxy tab 2. The GPS sample is runnable, it dooesn’t word though. When I run it on the devide, I just see dashes instead of coordinates. The permissions are set correctly.

 usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_FINE\_LOCATION", "android.permission.ACCESS\_COARSE\_LOCATION", }, },

Thanks,

Rik

Can you post your whole build.settings?

Are you looking at your device’s console log for errors?  If you don’t know how, please read:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

The GPS sample app only works on a device with GPS support.  I don’t know much about the device you are using. Are there any settings that have to be turned on to enable the GPS?

Hi @rikben,

Can you please post more of your code, including where you set up the listener function? We (or other developers) will need more info to assist you on this.

Thanks,

Brent

Hi,

Thanks for getting back to me. I’ll post my code. I copied some content from the docs. The permissions are set correctly.

----------------------------------------------------------------------------------------- -- -- scene2.lua -- ----------------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local function returnHome() storyboard.gotoScene( "splash", "crossFade", 1000 ) return true end function scene:createScene( event ) local group = self.view local background = display.newImage("workscreen.png") background.x = display.contentWidth/2 background.y = 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 local latitudeText = string.format( '%.4f', event.latitude ) currentLatitude = latitudeText latitude:setText( latitudeText ) local longitudeText = string.format( '%.4f', event.longitude ) currentLongitude = longitudeText longitude:setText( longitudeText ) local altitudeText = string.format( '%.3f', event.altitude ) altitude:setText( altitudeText ) local accuracyText = string.format( '%.3f', event.accuracy ) accuracy:setText( accuracyText ) local speedText = string.format( '%.3f', event.speed ) speed:setText( speedText ) local directionText = string.format( '%.3f', event.direction ) direction:setText( directionText ) -- Note that 'event.time' is a Unix-style timestamp, expressed in seconds since Jan. 1, 1970 local timeText = string.format( '%.0f', event.time ) time:setText( timeText ) end end -- Activate location listener Runtime:addEventListener( "location", locationHandler ) local lat = event.latitude local long = event.longitude local myText = display.newText( "Lat: " .. lat, 100, 200, native.systemFont, 16 ) myText:setFillColor( 0, 0, 0 ) print( lat ) print ( long ) appicon = widget.newButton{ defaultFile="code\_but.png", onRelease = returnHome } appicon.x = 1015 appicon.y = 643 group:insert ( background ) group:insert ( appicon ) end function scene:enterScene( event ) local group = self.view end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view if appicon then appicon:removeSelf() appicon = nil end end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Thanks,

Rik

Are you trying to run this in the simulator?  Are you building for a device?  What device?  If you’re doing android do you have all the right permissions in your build.settings?

Can you build and run the GPS sample app?

Rob

Hi,

I have tried to run it in the simulator. That doesn’t work. I am developping for a Samsung galaxy tab 2. The GPS sample is runnable, it dooesn’t word though. When I run it on the devide, I just see dashes instead of coordinates. The permissions are set correctly.

 usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_FINE\_LOCATION", "android.permission.ACCESS\_COARSE\_LOCATION", }, },

Thanks,

Rik

Can you post your whole build.settings?

Are you looking at your device’s console log for errors?  If you don’t know how, please read:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

The GPS sample app only works on a device with GPS support.  I don’t know much about the device you are using. Are there any settings that have to be turned on to enable the GPS?