GPS speed is extremely unnacurate

(English from translator probably some word is wrong lol)
hey guys, I’m doing a school work using corona, i use gps speed to fire a simple image and song, but this extremely inaccurate, 90% times it returns 0 speed, even if i run with my cellphone, heres the code:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
--SCRIPT POR KKRAFT PARA TRABALHO DE CIENCIAS!

local fundo = display.newImage("fundo.png")
local img = display.newImage("rosto.png")
local imghappy = display.newImage("rostohappy.png")

--Inicio
fundo.width = 10000
fundo.height = 10000
img.width = 350
img.height = 300
imghappy.width = 350
imghappy.height = 300
imghappy.isVisible = false
img.x = display.contentCenterX - 5
img.y = display.contentCenterY
imghappy.x = display.contentCenterX - 5
imghappy.y = display.contentCenterY

--Vibração

local function acelerometro(event)
    if event.zGravity >= 0.1 or event.xGravity >= 0.1 then
    system.vibrate( )
end
end
local function speedfunc(event)
    print( event.speed )
    if event.speed >= 0.0001 then
    local enginefx = audio.loadSound( "alfa-romeo-c.mp3")
    audio.play(enginefx,{duration=1900})
    imghappy.isVisible = true
    img.isVisible = false

end
    end
local function reset()
    imghappy.isVisible = false
    img.isVisible = true
    end
imghappy:addEventListener( "touch", reset )
Runtime:addEventListener("accelerometer", acelerometro )
Runtime:addEventListener( "location", speedfunc )

Please help me i have to bring it to the teacher Monday

Welcome to the community.

Please tell us what device you’re using.

There are a lot of factors that can affect accuracy of GPS (GPS.gov: GPS Accuracy).

You should print the:

Also, be sure you’re using the right build.settings: Solar2D Documentation — API Reference | Events | location

I would turn off: "android.permission.ACCESS_COARSE_LOCATION" ,

Oh, and don’t forget if you’ve disabled GPS and/or location services via settings on your phone, GPS probably won’t work.

Updates below to add timer-delta

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
--SCRIPT POR KKRAFT PARA TRABALHO DE CIENCIAS!

local fundo = display.newImage("fundo.png")
local img = display.newImage("rosto.png")
local imghappy = display.newImage("rostohappy.png")
local timeLabel = display.newText( "waiting", 100, 100 )
local accuracyLabel = display.newText( "waiting", 100, 120 )
local errorLabel = display.newText( "waiting", 100, 140 )
local lastTime = 0;

--Inicio
fundo.width = 10000
fundo.height = 10000
img.width = 350
img.height = 300
imghappy.width = 350
imghappy.height = 300
imghappy.isVisible = false
img.x = display.contentCenterX - 5
img.y = display.contentCenterY
imghappy.x = display.contentCenterX - 5
imghappy.y = display.contentCenterY

--Vibração

local function acelerometro(event)
    if event.zGravity >= 0.1 or event.xGravity >= 0.1 then
    system.vibrate( )
end
end
local function speedfunc(event)
    print( event.speed )
    if event.speed >= 0.0001 then
    local enginefx = audio.loadSound( "alfa-romeo-c.mp3")
    audio.play(enginefx,{duration=1900})
    imghappy.isVisible = true
    img.isVisible = false

    -- Calculate delta time and update label
    local deltaTime = event.time - lastTime
    lastTime = event.time
    timeLabel.text = string.format( "delta = %d ", deltaTime )

    -- Update other labels
     errorLabel.text = event.errorCode and "Error Code: " .. event.errorCode or "No error code"
     accuracyLabel.text = event.accuracy and "Accuracy: " .. event.accuracy or "No accuracy value?"

end
    end
local function reset()
    imghappy.isVisible = false
    img.isVisible = true
    end
imghappy:addEventListener( "touch", reset )
Runtime:addEventListener("accelerometer", acelerometro )
Runtime:addEventListener( "location", speedfunc )
2 Likes

Also, have you run the GPS example that comes with Corona simulator built-in ‘Samples’ on your phone?

If the sample app works and yours doesn’t you can compare the code to see what is different.

THANKS A LOT MAN, i ve trying that for almost 2 months, I understood that the error is the speed value is a string, so a added a tonumber and it works!(i just realized it cause you used string.format, so thanks dude you saved me)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.