How to reset the location or pull the current location without having to exit the app

This sends “or should send” a users location every 15 seconds. Everything is firing correctly except it is sending the same location until the app is closed and re-opened. If you fully exit the app and come back the location will be correct one time.

How to reset the location or pull the current location without having to exit the app

local composer = require( "composer" ) local scene = composer.newScene() local widget = require( "widget" ) local json = require ("json") local decodedData local muserID = system.getInfo( "deviceID" ) local reXlistener = {} local gotolocation = {} local latitude = display.newText( "", -100, -100, native.systemFont, 1 ) local longitude = display.newText( "-", -100, -100, native.systemFont, 1 ) local altitude = display.newText( "", -100, -100, native.systemFont, 1 ) local accuracy = display.newText( "", -100, -100, native.systemFont, 1 ) local speed = display.newText( "", -100, -100, native.systemFont, 1 ) local direction = display.newText( "", -100, -100, native.systemFont, 1 ) local time = display.newText( "", -100, -100, native.systemFont, 1 ) local timesTwoRun = 10 system.setIdleTimer( false ) function scene:create( event ) local sceneGroup = self.view background = display.newImageRect( "images/bg.png", 318, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) logoBtn = display.newImageRect("images/rexlogo.png", 260, 198) logoBtn.x = centerX -4 logoBtn.y = 115 sceneGroup:insert(logoBtn) local highlight = display.newImage ("images/highlight.png",480,644) highlight.x = display.contentWidth /2+300 highlight.y = display.contentHeight/2 highlight.alpha = 0.4 highlight.rotation = -75 transition.to(highlight, {delay=1200, alpha=0.0,xScale=10.0, yScale=32.0, x=90, time=2200}) -------------------------------------------- local function gotoCancel( self, event ) if event.phase == "began" then timer.cancel(rexTimer) timer.cancel(rexPanic) composer.gotoScene( "home", "crossFade", 800 ) return true end end completeBtn = display.newImageRect("images/panic-cancel.png", 200, 223) completeBtn.x = centerX completeBtn.y = 338 completeBtn.alpha = 0 sceneGroup:insert(completeBtn) completeBtn.touch = gotoCancel completeBtn:addEventListener( "touch", completeBtn ) local function makeREXConnections( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response local tninja = json.decode( event.response ) for key in pairs(tninja) do local server\_connected = tninja[key]["server\_connected"] -- currentThis = tonumber(server\_connected) transition.to( completeBtn, { delay=10, effect = "fade", time=3300, alpha=9} ) end end end local function gotoREX( event ) network.request( "http://www.domain.com/checkin.php?f=hw&rex\_type=2&onlyrunonce=1&u\_device="..muserID.."&xsaccuracy=".. accuracy.text .."&xsspeed=".. speed.text .."&xslat=".. latitude.text .."&xslog=".. longitude.text, "GET", makeREXConnections ) end --local gotolocation = function( event ) local function gotolocation( event ) if ( event.errorCode ) then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) print( "Location error: " .. tostring( event.errorMessage ) ) else native.showAlert( "GPS Location ", event.latitude, {"OK"} ) latitudeText = string.format( '%.4f', event.latitude ) latitude.text = latitudeText longitudeText = string.format( '%.4f', event.longitude ) longitude.text = longitudeText accuracyText = string.format( '%.3f', event.accuracy ) accuracy.text = accuracyText speedText = string.format( '%.3f', event.speed ) speed.text = speedText timeText = string.format( '%.0f', event.time ) time.text = timeText print(latitude.text) print(time.text) gotoREX() Runtime:removeEventListener( "location", gotolocation) end end function reXlistener:timer( event ) Runtime:addEventListener( "location", gotolocation) end end function scene:show( event ) local phase = event.phase if "did" == phase then rexTimer = timer.performWithDelay( 1000, reXlistener, 1 ) rexPanic = timer.performWithDelay( 15000, reXlistener, timesTwoRun ) end if "will" == phase then transition.to( completeBtn, { delay=1, effect = "fade", time=1, alpha=0} ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:destroy( event ) local sceneGroup = self.view transition.to( completeBtn, { delay=1, effect = "fade", time=1, alpha=0} ) composer.removeScene( "checkin", false ) end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

55 views after several days but no reply? Love corona but it seems like the support is limited. Can I pay for support?

You can always bump your threads if they’ve not been responded to in a day or so.  What will help is a better description of what you’re trying to do.  What platform you’re trying to do this on, etc.

Also go ahead and post your build.settings file.

Rob

Rob, I’m not sure how this would be a build issue but here it is. I’m not worried about Apple at this point so I haven’t added that to the build.

Everything seem to be firing off correctly. It is send to the server as it should everything is starting and stopping correctly.

My issue is the location does not update. Stays the same unless I fully exit the app. Runtime listener?

Simulator
2014.2511 (2014.11.18)

settings = { plugins = { -- Add Google Play Services ["plugin.google.play.services"] = { publisherId = "com.coronalabs" }, }, orientation = { default = "portrait", supported = { "portrait", } }, android = { usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_FINE\_LOCATION", "android.permission.ACCESS\_COARSE\_LOCATION", }, usesFeatures = { { name = "android.hardware.location", required = false }, { name = "android.hardware.location.gps", required = false }, { name = "android.hardware.location.network", required = false } }, }, }

Please tell me what you’re trying to do. 

Rob

Thanks for your attention on this!

I need the location to update. network.request is firing off every 15 seconds using the gotoREX located in gotolocation function.

The only issue I’m seeing is the location does not update. If I navigate away from this scene and then come back later every else resets. But the location does not. I need to fully close the app and restart it for the location to update.

I want the location to update each time the gotolocation function is called. It is called every 15 second when the scene is open for  30 minutes. Leaving the scene stops the timer and going back to the scene reloads the timer.

This is the app. I need to pull it from Google Play quickly if I cant get the location to work correctly.

https://play.google.com/store/apps/details?id=com.howeyweb.mike.REX_GPS_Child_Guardian&hl=en

local function gotoREX( event ) network.request( "http://www.domain.com/checkin.php?f=hw&rex\_type=2&onlyrunonce=1&u\_device="..muserID.."&xsaccuracy=".. accuracy.text .."&xsspeed=".. speed.text .."&xslat=".. latitude.text .."&xslog=".. longitude.text, "GET", makeREXConnections ) end local function gotolocation( event ) if ( event.errorCode ) then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) print( "Location error: " .. tostring( event.errorMessage ) ) else native.showAlert( "GPS Location ", event.latitude, {"OK"} ) latitudeText = string.format( '%.4f', event.latitude ) latitude.text = latitudeText longitudeText = string.format( '%.4f', event.longitude ) longitude.text = longitudeText accuracyText = string.format( '%.3f', event.accuracy ) accuracy.text = accuracyText speedText = string.format( '%.3f', event.speed ) speed.text = speedText timeText = string.format( '%.0f', event.time ) time.text = timeText print(latitude.text) print(time.text) gotoREX() Runtime:removeEventListener( "location", gotolocation) end end function reXlistener:timer( event ) Runtime:addEventListener( "location", gotolocation) end

Are you intending for this to send out requests while the app is suspended?

Corona SDK does not support processing while in the background.  Or are you just depending on the GPS while in the foreground and it’s not restarting?

Rob

It only needs to function while the user has the app open. I wouldn’t want it to function while the app is in the background. I don’t want a stalker app.  In the foreground only

Rob, I think my original question was somewhat clear and the code I pasted is very basic. Seems like a simple question for the Corona people.

The only thing I see that’s suspicious is that you’re turning on a listener, and then immediately removing it after the event fires.  If the GPS errors along the way, you don’t remove the listener and it will keep firing.  When you change scenes that code may not be around for the event handler to run and it could cause an error.  I also don’t know of the underling GPS code is designed to be churned like that.  Also your two timer handles are global variables and perhaps that’s creating your issues.

Rob

I’m super new to corona and I think my 15+ years of programming is hurting me more than helping me. A task that should be simple is over complicated with every process in lua. Leaving a scene should stop the scene without additional work on our end. Now, if we want something to stay alive than we should address it. Not the other way around that’s just silliness.

Would you suggest that I activate the location and just let it run every few seconds? I guess doing this would allow me to obtain and send the info when I need it. My worry with that is it may drain the users battery. However from what I’ve read when the app is out of view or closed the GPS won’t run. On my Galaxy S3 the GPS stay alive in the notifications bar when which leads me to believe it’s still running.

The GPS should not run while it’s backgrounded.  I would start it and let it update as it does.  Then just run your timer to transmit your data every 15 seconds.

Rob

Thank you Rob, almost have it now.

55 views after several days but no reply? Love corona but it seems like the support is limited. Can I pay for support?

You can always bump your threads if they’ve not been responded to in a day or so.  What will help is a better description of what you’re trying to do.  What platform you’re trying to do this on, etc.

Also go ahead and post your build.settings file.

Rob

Rob, I’m not sure how this would be a build issue but here it is. I’m not worried about Apple at this point so I haven’t added that to the build.

Everything seem to be firing off correctly. It is send to the server as it should everything is starting and stopping correctly.

My issue is the location does not update. Stays the same unless I fully exit the app. Runtime listener?

Simulator
2014.2511 (2014.11.18)

settings = { plugins = { -- Add Google Play Services ["plugin.google.play.services"] = { publisherId = "com.coronalabs" }, }, orientation = { default = "portrait", supported = { "portrait", } }, android = { usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_FINE\_LOCATION", "android.permission.ACCESS\_COARSE\_LOCATION", }, usesFeatures = { { name = "android.hardware.location", required = false }, { name = "android.hardware.location.gps", required = false }, { name = "android.hardware.location.network", required = false } }, }, }

Please tell me what you’re trying to do. 

Rob

Thanks for your attention on this!

I need the location to update. network.request is firing off every 15 seconds using the gotoREX located in gotolocation function.

The only issue I’m seeing is the location does not update. If I navigate away from this scene and then come back later every else resets. But the location does not. I need to fully close the app and restart it for the location to update.

I want the location to update each time the gotolocation function is called. It is called every 15 second when the scene is open for  30 minutes. Leaving the scene stops the timer and going back to the scene reloads the timer.

This is the app. I need to pull it from Google Play quickly if I cant get the location to work correctly.

https://play.google.com/store/apps/details?id=com.howeyweb.mike.REX_GPS_Child_Guardian&hl=en

local function gotoREX( event ) network.request( "http://www.domain.com/checkin.php?f=hw&rex\_type=2&onlyrunonce=1&u\_device="..muserID.."&xsaccuracy=".. accuracy.text .."&xsspeed=".. speed.text .."&xslat=".. latitude.text .."&xslog=".. longitude.text, "GET", makeREXConnections ) end local function gotolocation( event ) if ( event.errorCode ) then native.showAlert( "GPS Location Error", event.errorMessage, {"OK"} ) print( "Location error: " .. tostring( event.errorMessage ) ) else native.showAlert( "GPS Location ", event.latitude, {"OK"} ) latitudeText = string.format( '%.4f', event.latitude ) latitude.text = latitudeText longitudeText = string.format( '%.4f', event.longitude ) longitude.text = longitudeText accuracyText = string.format( '%.3f', event.accuracy ) accuracy.text = accuracyText speedText = string.format( '%.3f', event.speed ) speed.text = speedText timeText = string.format( '%.0f', event.time ) time.text = timeText print(latitude.text) print(time.text) gotoREX() Runtime:removeEventListener( "location", gotolocation) end end function reXlistener:timer( event ) Runtime:addEventListener( "location", gotolocation) end

Are you intending for this to send out requests while the app is suspended?

Corona SDK does not support processing while in the background.  Or are you just depending on the GPS while in the foreground and it’s not restarting?

Rob

It only needs to function while the user has the app open. I wouldn’t want it to function while the app is in the background. I don’t want a stalker app.  In the foreground only