Hi,
In this scene I try to get users to submit location of intresting places. That works. But if they do it twice, without restart the app, there is no GPS information on the second submission.
I am very new to this, so the code is a bit of a mess, sorry about that. I guess I have placed the GPS function in the wrong part of the scene. If anyone could see what I should change I would be very thankful.
Thanks !
local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- -- local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 ) local background = display.newImageRect( sceneGroup, "background.PNG", display.contentWidth, display.contentHeight ) background.x = display.contentCenterX background.y = display.contentCenterY local titletext = display.newText( sceneGroup, "- Create POI -", 300, 80, native.systemFont, 60) titletext.x = display.contentCenterX titletext.y = 80 titletext:setFillColor(0, 1, 0) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- DISPLAY GPS LOCATION -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local latitude = display.newText( "-", 100, 50, native.systemFont, 16 ) local longitude = display.newText( "-", 100, 100, native.systemFont, 16 ) local altitude = display.newText( "-", 100, 150, native.systemFont, 16 ) local speed = display.newText( "-", 100, 250, native.systemFont, 16 ) 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 ) latitude.text = latitudeText local longitudeText = string.format( '%.4f', event.longitude ) longitude.text = longitudeText local altitudeText = string.format( '%.3f', event.altitude ) altitude.text = altitudeText local speedText = string.format( '%.3f', event.speed ) speed.text = speedText end end -- Get the table of current values for all columns -- This can be performed on a button tap, timer execution, or other event -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- COMMENT TEXTBOX -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local defaultBox local function textListener( event ) if ( event.phase == "began" ) then -- User begins editing "defaultBox" elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- Output resulting text from "defaultBox" print( event.target.text ) elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) end end -- Create text box defaultBox = native.newTextBox( 200, 200, 450, 250 ) defaultBox.text = "Add comment here " defaultBox.isEditable = true defaultBox.x = display.contentCenterX defaultBox.y = 400 defaultBox.font = native.newFont( native.systemFontBold, 30 ) -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- SEND REPORT BUTTON -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local widget = require( "widget" ) -- Function to handle button events local function handleSettingsButtonEvent( event ) if ( "ended" == event.phase ) then print( "Send Report Button was pressed" ) local json = require( "json" ) local mime = require( "mime" ) local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else print ( "RESPONSE: " .. event.response ) end end local body = {} body.resource = {} body.resource.gpslat = latitude.text body.resource.gpslon = longitude.text body.resource.gpsspeed = speed.text body.resource.gpsalt = altitude.text body.resource.alliance = system.getPreference( "app", "alliance", "string" ) body.resource.nic = system.getPreference( "app", "nic", "string" ) body.resource.comment = defaultBox.text body = json.encode(body) local headers = {} headers["X-DreamFactory-Api-Key"] = "\<key here\>" headers["Authorization"] = "Basic \<ABC\>" headers["Content-Type"] = "application/json" local params = {} params.headers = headers params.body = body network.request( "http://server.domain/api/v2/cstri-mysql01/\_table/poi", "POST", networkListener, params ) composer.gotoScene( "menu" ) end end -- Create the widget local settingsbutton = widget.newButton( { label = "button", onEvent = handleSettingsButtonEvent, emboss = false, -- Properties for a rounded rectangle button shape = "roundedRect", width = 600, height = 60, cornerRadius = 2, fillColor = { default={0, 0.50196078431373, 0, 1}, over={1,0.1,0.7,0.4} }, strokeColor = { default={0, 1, 0, 10}, over={0.8,0.8,1,1} }, labelColor = { default={ 0, 0, 0 }, over={ 1, 1, 0.87843137254902, 0.5 } }, strokeWidth = 4 } ) -- Center the button settingsbutton.x = display.contentCenterX settingsbutton.y = 700 -- Change the button's label text settingsbutton:setLabel( "Submit and return to menu" ) settingsbutton.\_view.\_label.size = 40 -- Adding the button to scene group sceneGroup:insert(settingsbutton) -- Activate location listener Runtime:addEventListener( "location", locationHandler ) end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene