GPS

Features demonstrated:

Locations events, buttons, touch

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
-- Load external button/label library (ui.lua should be in the same folder as main.lua)
local ui = require("ui")
 
local currentLatitude = 0
local currentLongitude = 0
 
display.setStatusBar( display.HiddenStatusBar )
 
local background = display.newImage("gps_background.png")
 
 
local latitude = ui.newLabel{
        bounds = { 136, 51, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
}
 
local longitude = ui.newLabel{
        bounds = { 136, 101, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
}
 
local altitude = ui.newLabel{
        bounds = { 136, 151, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
}
 
local accuracy = ui.newLabel{
        bounds = { 136, 201, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
}
 
local speed = ui.newLabel{
        bounds = { 136, 251, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
        }
 
local direction = ui.newLabel{
        bounds = { 136, 301, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
}
 
local time = ui.newLabel{
        bounds = { 136, 351, 180, 40 },
        text = "--",
        font = "DBLCDTempBlack",
        textColor = { 255, 85, 85, 255 },
        size = 26,
        align = "left"
}
 
 
local buttonPress = function( event )
        -- Show location on map
        mapURL = "http://maps.google.com/maps?q=Hello,+Corona!@" .. currentLatitude .. "," .. currentLongitude
        system.openURL( mapURL )
end
 
 
local button1 = ui.newButton{
        default = "buttonRust.png",
        over = "buttonRustOver.png",
        onPress = buttonPress,
        text = "Show on Map",
        font = "TrebuchetMS-Bold",
        size = 22,
        textColor = { 200, 200, 200, 255 },
        emboss = true,
        x = 160,
        y = 422
}
 
local locationHandler = function( event )
        local latitudeText = string.format( '%.4f', event.latitude )
        currentLatitude = latitudeText
        latitude:setText( latitudeText )
        
        local longitudeText = string.format( '%.4f', event.longitude )
        currentLongitude = longitudeText
        longitude:setText( longitudeText )
        
        local altitudeText = string.format( '%.3f', event.altitude )
        altitude:setText( altitudeText )
 
        local accuracyText = string.format( '%.3f', event.accuracy )
        accuracy:setText( accuracyText )
        
        local speedText = string.format( '%.3f', event.speed )
        speed:setText( speedText )
 
        local directionText = string.format( '%.3f', event.direction )
        direction:setText( directionText )
 
        -- Note: event.time is a Unix-style timestamp, expressed in seconds since Jan. 1, 1970
        local timeText = string.format( '%.0f', event.time )
        time:setText( timeText )
end
 
-- Determine if running on Corona Simulator
--
local isSimulator = "simulator" == system.getInfo("environment")
 
-- Location Events is not supported on Simulator
--
if isSimulator then
        msg = display.newText( "Location events not supported on Simulator!", 0, 230, "Verdana-Bold", 13 )
        msg.x = display.contentWidth/2          -- center title
        msg:setTextColor( 255,255,255 )
end
 
-- Activate location listener
Runtime:addEventListener( "location", locationHandler )