LUA example: Check our GPS position

In this example we’ll see how to get the phone’s current GPS position and check that our operator really is where they should be. In practice, when they save their work report, we’ll check whether they are actually near the customer.

Script Group
Form
Event
Post-save

We add the two functions the script uses to calculate distances and to check whether the GPS point is actually inside the required radius

 -- distance between 2 points
function get_distance(A,B)
   local latA = (A["lat"] * math.pi) / 180
   local lonA = (A["lon"] * math.pi) / 180
   local latB = (B["lat"] * math.pi) / 180
   local lonB = (B["lon"] * math.pi) / 180
   local R = 6372.795477598
   local dist = (R * math.acos(math.sin(latA) *
   math.sin(latB) + math.cos(latA) * math.cos(latB) * math.cos(lonA-lonB)))*1000
   return dist
end

-- check whether the position passed is on the work site
function in_cantiere(coordA, coordB, raggio)
   if (get_distance(coordA, coordB)<=raggio) then
      return true
   end
   return false
end

First we create two functions that the script calls to measure the distances between the current GPS point and, in this case, the GPS points of the work sites.

local cantieri = database.getsql("SELECT * FROM coordinate_cantieri WHERE latitudine<>0 AND longitudine<>0 AND latitudine IS NOT NULL AND longitudine IS NOT NULL")

local righe_cantieri = cantieri.getrows()

-- get the GPS coordinates
local coord = program.geolocation()
local curr_lat = 0
local curr_lon = 0

curr_lat = coord[1]
curr_lon = coord[2]

We read all the work sites in the database, taking only those with a valid GPS coordinate. Nios4 ignores latitude and longitude values when both are zero.

Then we query the phone for its position.

--coordinates not found
if (curr_lat==0 and curr_lon==0) then
   errorn.errorcode = "ERR-FL"
   errorn.errormessage = "Unable to get the GPS coordinates, please try again later"
   dataview.setvalue("stato","To be checked (GPS not found)")
   do return end
end

If the GPS point can’t be retrieved, we can raise an error and exit the script. This is the most drastic approach.

A better approach would be to show a message and still allow saving, perhaps setting a field to data not confirmed. This avoids an operator being unable to save their data when the GPS isn’t working.

-- current position of the device
local current_coord = {lat = curr_lat, lon = curr_lon}

for i, c in pairs(righe_cantieri) do
   -- work site coordinates
   local lat = c.getvalue("latitudine")
   local lon = c.getvalue("longitudine")
   local cantiere_coord = {lat = lat, lon = lon}

   if (in_cantiere(current_coord, cantiere_coord, c.getvalue("raggio"))==true) then
      dataview.setvalue("stato","GPS found")
      -- here you can link the work site to the report, for example by saving c.getvalue("gguidp") in a field
     do return end
   end
end

dataview.setvalue("stato","To be checked (GPS not found)")

We go through all the work sites and, using the functions created at the start, check whether the distance between our current position and each site is less than the given radius. If it is, we really are on the site; otherwise the field is set to show that the values were not confirmed by the GPS position.