This code works for iOS but not for Android.
What am I doing wrong?
--------- build.settings -----
settings =
{
android =
{
usesPermissions =
{
“android.permission.INTERNET”,
“android.permission.WRITE_EXTERNAL_STORAGE”,
“android.permission.ACCESS_FINE_LOCATION”,
“android.permission.ACCESS_COURSE_LOCATION”,
},
},
iphone =
{
plist =
{
CFBundleIconFile = “Icon.png”,
CFBundleIconFiles = {
“Icon.png”,
“Icon@2x.png”,
“Icon-60.png”,
“Icon-60@2x.png”,
“Icon-72.png”,
“Icon-72@2x.png”,
“Icon-76.png”,
“Icon-76@2x.png”,
“Icon-Small.png”,
“Icon-Small@2x.png”,
“Icon-Small-40.png”,
“Icon-Small-40@2x.png”,
“Icon-Small-50.png”,
“Icon-Small-50@2x.png”,
},
},
}
}
------- main.lua -----
–
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
display.setStatusBar( display.DefaultStatusBar )
display.setDefault( “background”, 200 )
local myText = display.newText( “TESTE GPS”, 0, 0, native.systemFont, 40 )
myText.x = centerX
myText.y = display.contentWidth / 4
myText:setFillColor( 1, 110/255, 110/255 )
local myCoord = display.newText( “TESTE GPS”, 0, 0, native.systemFont, 20 )
myCoord.x = centerX
myCoord.y = 3*display.contentWidth / 4
myCoord:setFillColor( 1, 110/255, 110/255 )
local currentLatitude = 0
local currentLongitude = 0
local coords = ‘’
local updateGps = 5000 – update Gps every X seconds
local locationHandler = function( event )
– On update, stop listening to GPS signal to avoid battery draining
Runtime:removeEventListener( “location”, locationHandler )
– Check for error (user may have turned off Location Services)
if event.errorCode then
print( "Location error: " … tostring( event.errorMessage ) )
else
currentLatitude = string.format( ‘%.4f’, event.latitude )
currentLongitude = string.format( ‘%.4f’, event.longitude )
coords = currentLatitude … “,” … currentLongitude
myCoord.text = coords
end
end
local function updateGPSTimer()
Runtime:addEventListener( “location”, locationHandler )
timer.performWithDelay( 5000, updateGPSTimer )
print (coords)
end
– Start GPS timer
updateGPSTimer()