Location (GPS) returning wrong values - android

I'm just learning the Corona SDK for coding some useful things for private use on my Xperia Z1 compact with Android 5.1.1
I stumbled across the functions for location and gave it a try (with massive help from the reference and the examples).
Latitude, longitude and accuracy and work just fine. The coordinates are very close to my actual position (about 10 metres) and they are within the given accuracy (31.5 metres). However, Altitude, Speed and Direction aren't quite right.
When I start the application, the altitude is "right" (maybe 50 metres too high) at first, but after a few seconds it switches to zero and stays there. This also happens when I "minimise" the application for a short moment and re-open it again.
The speed and direction stay zero all the time, even when I move around.
Here is "my" (mostly copied) code:
local display = require ("display")
local t0 = display.newText("Latitude", 100, 50, native.systemFont, 16)
local t1 = display.newText("Longitude", 100, 100, native.systemFont, 16)
local t2 = display.newText("Altitude", 100, 150, native.systemFont, 16)
local t3 = display.newText("Accuracy", 100, 200, native.systemFont, 16)
local t4 = display.newText("Speed", 100, 250, native.systemFont, 16)
local t5 = display.newText("Direction", 100, 300, native.systemFont, 16)
local latitude = display.newText( "-", 200, 50, native.systemFont, 16 )
local longitude = display.newText( "-", 200, 100, native.systemFont, 16 )
local altitude = display.newText( "-", 200, 150, native.systemFont, 16 )
local accuracy = display.newText( "-", 200, 200, native.systemFont, 16 )
local speed = display.newText( "-", 200, 250, native.systemFont, 16 )
local direction = display.newText( "-", 200, 300, native.systemFont, 16 )
local locationHandler = function( event )
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 accuracyText = string.format( '%.3f', event.accuracy )
accuracy.text = accuracyText
local speedText = string.format( '%.3f', event.speed )
speed.text = speedText
local directionText = string.format( '%.3f', event.direction )
direction.text = directionText
end
Runtime:addEventListener( "location", locationHandler )
THANKS!

It all seems right.
1- Altitude with this type of gps wont be accurate, 50m is good accuracy.
2- You have to add direction listener for direction to work:
local directionHandler1 = function( event )
local magneticText = string.format( '%.1f', event.magnetic )
magnetic.text = magneticText
end
Runtime:addEventListener( "heading", directionHandler1 )
You need all these three:
Runtime:addEventListener( "location", locationHandler1 )
Runtime:addEventListener( "heading", directionHandler1 )
Runtime:addEventListener('accelerometer', processor)

Related

Can't Move Group backwards! Corona SDK

i'm new here, i'm trying to do a videogame for android with corona sdk, but i'm stuck with the creation of random particle on the background,
here is the code:
local menu = composer.newScene()
function menu:create( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
blackLayer = display.newImageRect( "black_layer.png", 1280, 300 )
blackLayer.y = display.contentCenterY + 150
blackLayer.x = display.contentCenterX
blackLayer.alpha = 0.6
background = display.newImageRect( "background.png", 1280 , 720 )
background.x = display.contentCenterX
background.y = display.contentCenterY
playBtn = widget.newButton( {
id="playBtn",
x=display.contentCenterX+350,
y=blackLayer.y,
labelColor = { default={0.922, 0.938, 0.941}, over={0.9, 0.3, 0.234, 0.6}},
label = "Play Now",
defaultFile = "redBtn.png",
overFile = "whiteBtn.png",
width = 200,
height = 200,
fontSize = 30,
font = "Fonts/Raleway-Bold.ttf",
--onRelease = composer.gotoScene( "game")
} )
settingsBtn = widget.newButton( {
id="playBtn",
x=display.contentCenterX,
y=blackLayer.y-50,
font = "Fonts/Raleway-Bold.ttf",
label = "Settings",
defaultFile = "rectWhiteBtn.png",
overFile = "rectRedBtn.png",
width =250,
height = 100,
fontSize = 30,
labelColor = { default={0.9, 0.3, 0.234} , over={0.922, 0.938, 0.941, 0.6} },
onRelease = gotoSettings
} )
highScoresBtn = widget.newButton( {
id="highScoresBtn",
x=display.contentCenterX,
y=settingsBtn.y + 100,
font = "Fonts/Raleway-Bold.ttf",
label = "High Scores",
defaultFile = "rectWhiteBtn.png",
overFile = "rectRedBtn.png",
width =250,
height = 100,
fontSize = 30,
labelColor = { default={0.9, 0.3, 0.234} , over={0.922, 0.938, 0.941, 0.6} },
--onRelease = composer.gotoScene("highScores")
} )
bg:insert(background)
sceneGroup:insert(blackLayer)
sceneGroup:insert(playBtn)
sceneGroup:insert(settingsBtn)
sceneGroup:insert(highScoresBtn)
function generateParticles()
local xy=math.random(30,70)
local newParticle = display.newImageRect( particlesGroup, objectSheet , math.random(13), xy, xy )
newParticle.x = (math.random(1280)*math.random(88932))%1280
newParticle.y = (math.random(720)*math.random(13546))%720
newParticle.alpha = 0
transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
end
function generateParticlesSmall()
local xy=math.random(5,15)
local newParticle = display.newImageRect( particlesGroup, objectSheet , 1, xy, xy )
newParticle.x = (math.random(1280)*math.random(88932))%1280
newParticle.y = (math.random(720)*math.random(13546))%720
newParticle.alpha = 0
transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
end
end
function menu:show( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
bigPTimer1=timer.performWithDelay( 1, generateParticles, 0 )
-- Start the timer wich call 'generateParticles' for each ms
smallPTimer=timer.performWithDelay( 1, generateParticlesSmall, 0 )
-- Start the timer wich call 'generateParticlesSmall' for each ms
particlesGroup:toBack()
elseif ( phase == "did" ) then
if event.phase=="began"then
end
if event.phase=="ended"then
end
end
end
function menu:hide( event )
local bg = self.view
local particlesGroup = self.view
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
end
end
function menu:destroy( event )
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
end
--Funzioni
local function gotoGame()
composer.gotoScene( "game" )
end
local function gotoHighScores()
composer.gotoScene( "highscores" )
end
function gotoSettings()
reproduceEffet1()
timer.cancel(bigPTimer1)
timer.cancel(smallPTimer)
composer.gotoScene( "settings", options)
end
--funzioni
local function removetotally( particle )
display.remove( particle )
end
function dimParticle( particle )
transition.to( particle, {alpha=0, time=500, onComplete = removetotally, onCompleteParams = particle})
end
--
menu:addEventListener( "create", menu )
menu:addEventListener( "show", menu )
menu:addEventListener( "hide", menu )
menu:addEventListener( "destroy", menu )
--
return menu
All functions are working perfectly, but the problem is that I can't move back the group of particles, between bg and sceneGroup.
Thank for listening me.
In your code you have only 1 display group.
local bg = self.view
local particlesGroup = self.view
local sceneGroup = self.view
You need to create particlesGroup as child in order to move it back:
Declare local variables on the top of the file:
local menu = composer.newScene()
local bg, particlesGroup
Create groups in menu:create
bg = display.newGroup()
particlesGroup = display.newGroup()
local sceneGroup = self.view
sceneGroup:insert(bg)
sceneGroup:insert(particlesGroup) -- here particlesGroup will be on the top
particlesGroup:toBack() --now particlesGroup is under bg

storyboard.removeScene("scene name") is not working

I am new to corona sdk and trying to build sample in it. i want to get back to main screen automatically after some time by destroying my scene(i mean that scene destroys itself after some particular time and user get back to main screen).I know that for some this may sound silly but i beginner to Corona
Here is my code of main.lua :-
local storyboard = require "storyboard"
local widget = require( "widget" )
--/**/ widget.setTheme( "widget_theme_android" )
local json = require "json"
local particleDesigner = require( "particleDesigner" )
local physics = require( "physics" )
physics.start()
-------------------------------------------------------------------------------
local sky = display.newImage( "sky.jpg",contentCenterX,display.contentCenterY)
local emitter
emitter = particleDesigner.newEmitter("air_stars.json")
emitter.x = (contentCenterX)
emitter.y = (contentCenterY)
local button = {}
y= -50
for count = 1,3 do
y = y + 110
x = 20
for insideCount = 1,3 do
x = x + 110
button[count] = display.newImage("1.png")
button[count].x = x
button[count].y = y
local container = display.newContainer( 0, 0 )
container:translate(button[count].x-40, button[count].y-40)
local bkgd = display.newImage( container, "2.png" )
function buttonTap(self)
button[count].touch = transition.to(container,{time=3000, height = button[count].height+x, width = button[count].width+y, onComplete = StartGame})
function StartGame()
storyboard.purgeScene("main")
if count == 1 and insideCount == 1 then
storyboard:gotoScene("bridge")
elseif count == 1 and insideCount == 2 then
storyboard:gotoScene("Heli")
end
end
end
button[count]:addEventListener( "touch", buttonTap)
end
end
return storyboard
And Here is my bridge.lua file :- (http://piratepad.net/ep/pad/view/ro.JR1Bpt1qkA$/latest)
Any help will be appreciated
Thanks
Hi sid here you go:
_W = display.contentWidth;
_H = display.contentHeight;
local button = {}
x = -20
for count = 1,3 do
x = x + 90
y = 20
for insideCount = 1,3 do
y = y + 90
button[count] = display.newImage("imgs/one.png");
button[count].x = x;
button[count].y = y;
local container = display.newContainer( 0, 0 )
container:translate(button[count].x, button[count].y)
local bkgd = display.newImage( container, "imgs/two.png" )
function buttonTap(self)
button[count].touch = transition.to(container,{time=3000, height = button[count].height+x, width = button[count].width+y})
end
button[count]:addEventListener( "touch", buttonTap )
end
end
just add this to the bridge.lua
local function goBack()
storyboard:gotoScene("buttons")
end
timer.performWithDelay( 3000, goBack, 1 )
and this to main.lua
storyboard.purgeOnSceneChange = true

I want to make a score count with a timer in Lua

I'm making a Lua game where when the scene starts, the timer starts increasing numbers for 20 or 30 points every second. What's the code for a timer like that?
Current score count (no count at all, just text)
scoreTxt = display.newText( "Score: 0", 0, 0, "Helvetica", 40 )
scoreTxt:setReferencePoint(display.TopLeftReferencePoint)
scoreTxt.x = display.screenOriginX + 10
scoreTxt.y = display.screenOriginY + 32
so the 'score: 0's number should increase every second...
local score = 0
local scoreTxt = display.newText( "Score: 0", 0, 0, "Helvetica", 40 )
scoreTxt:setReferencePoint(display.TopLeftReferencePoint)
scoreTxt.x = display.screenOriginX + 10
scoreTxt.y = display.screenOriginY + 32
local function updateScore()
score = score + 20
scoreText.text = string.format("Score: %d", score)
end
local scoreTimer = timer.performWithDelay(1000, updateScore, 0)
or something similar.
You can use the examples in the corona docs here and see if they can be adapted to your use case.

Test people standing still or motion in titanium

Now I want to do a app to test people standing still or motion in titanium..I use accelerometer and operated acceleration in 3 axis x,y,z..What I must use formula to test that problem..!
Another option would be to use GPS.
var win = Ti.UI.createWindow({ backgroundColor: 'white' });
// WARNING: This will only work well outside, where the phone can get a good GPS signal.
var label = Ti.UI.createLabel({ text: 'Traveled 0 ft' });
win.add(label);
// Set up the geolocation code
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 0.1;
Ti.Geolocation.purpose = 'To track how far you have traveled!';
var lastLocation = null, totalFtTraveled = 0;
/**
* This function is called by the phone every time we have new geolocation data. It writes out where the user currently is.
* #param e An argument given to us by the phone with information such as accuracy, latitude, and longitude.
*/
function reportPosition(e) {
if (!e.success || e.error) {
label.text = 'error: ' + JSON.stringify(e.error);
}
else {
if (lastLocation != null) {
var lat1 = lastLocation.latitude, lon1 = lastLocation.longitude;
var lat2 = e.coords.latitude, lon2 = e.coords.longitude;
var kmTraveled = 3963.0 * Math.acos(
Math.sin(lat1 / 57.2958) * Math.sin(lat2 / 57.2958)
+ Math.cos(lat1 / 57.2958) * Math.cos(lat2 / 57.2958)
* Math.cos(lon2 / 57.2958 - lon1 / 57.2958)
);
var ftTraveled = kmTraveled * 3280.8399;
totalFtTraveled += ftTraveled;
label.text = 'Traveled ' + totalFtTraveled + 'ft';
}
lastLocation = e.coords;
}
}
// This will get the location right now, and will get the phone ready to listen for the user's current location.
Ti.Geolocation.getCurrentPosition(reportPosition);
// And this fires whenever the "distance filter" is surpassed -- with a filter of 1, this happens every 1 meter or so.
Ti.Geolocation.addEventListener('location', reportPosition);
var reset = Ti.UI.createButton({ title: 'Reset', bottom: 10, height: 50, width: 200 });
reset.addEventListener('click', function() {
totalFtTraveled = 0;
label.text = 'Traveled 0ft';
});
win.add(reset);
win.open();
First, get scalar acceleration independent of movement direction by formula accel = sqrt(accel_x^2 + accel_y^2 + accel_z^2) where ^2 means power of 2 and sqrt means a square root. After that, apply some low-pass filter to get rid of random noise. Here is a good algorithm and using alpha=0.8 for it would be a good approach. Now, you can just see whether filtered acceleration is greater than some threshold value (sensitivity depends on this threshold). Good luck!

Measure volume on tap of button

Hey below is a code that measure volume at runtime. I want a button which when tapped once starts volume as in this code and then when tapped again stops measuring.
local g = display.newGroup()
local function newBar()
local bar = display.newLine( 0, 0, 1, 0 )
bar:setColor( 0, 55, 100, 20 )
bar.width = 7
bar.y=400
bar.x=20
return bar
end
local volumeBar = newBar()
volumeBar.y = 309
volumeBar.x = 320
function volumeBar:enterFrame ( event )
local v = 20*math.log(r:getTunerVolume())
local MINTHRESH = 20
local LEFTMARGIN = 20
local ONE = -1
local v2 = MINTHRESH + math.max (v, -MINTHRESH)
v2 = (display.contentWidth - 20 ) * v2 / MINTHRESH
local neg = math.max ( 20, v2 )
volumeBar.xScale = neg * ONE
if (v >= -4) then
volumeBar:setColor( 110, 110, 20, 200 )
elseif (v < -4) then
volumeBar:setColor( 235, 80, 80, 233)
end
end
Runtime:addEventListener("enterFrame", volumeBar)
g:insert(volumeBar)
Help please cause here there are 2 taps that need to be done.
Add a tap event listener to the button and call Runtime:removeEventListener("enterFrame", volumeBar) in the tap function.

Categories

Resources