Hello All,
I am working on a geo-referenced tour project that lets travelers see the various facets (wildlife, ecology, etc.) of a location as they walk a trail.
The overall goal is for the interpreter to automatically update the user’s session when their position (rounded to two decimal places) matches the coordinates listed as TADS3 rooms (probably defined as a ‘case statement’).
Here’s the pseudo code:
switch position
case(lat = -122.65 && lon = 45.63)
me.scriptedTravelTo(room_one);
case(lat = -122.78 && lon = 45.72)
me.scriptedTravelTo(room_two);
...
So far the project is going well but I’m having trouble passing regular GPS position updates to TADS3 for processing.
At this point I am using Ben Cressey’s excellent webscrptres extension [url]https://intfiction.org/t/invoking-js-directly-from-webui/4955/1] to run the JavaScript from a source file I created called ‘gps_daemon.t’.
Here’s what the GPS daemon file looks like (I’m using the DefineAction simply for testing, this will, of course, be a daemon in the production version):
#charset "utf-8"
#include <adv3.h>
#include <en_us.h>
DefineIAction(GPS);
VerbRule(GPS)
'gps'
: GPSAction
verbPhrase = 'script/scripting'
;
#define GPS(STR, ARGS...) JavaScript.eval( ## #@STR ##, ##ARGS ## )
modify GPSAction
execAction() {
GPS({
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
console.log('More or less ' + crd.accuracy + ' meters.');
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
navigator.geolocation.watchPosition(success, error, options);
});
}
;
Sure enough, as soon as I type ‘GPS’ into the interpreter, the JavaScript launches with position updates to the JavaScript console every 5,000 milliseconds:
Your current position is:
Latitude : 44.53651190000001
Longitude: -122.90703389999999
More or less 1449513 meters.
(The ‘1449513 meters’ accuracy is as a result of the position getting taken by my IP address rather than an actual GPS fix)
There are, however, a couple of problems with this approach.
First, there seems to be no way that I can see to pass the position to TADS3 by any means as a) the ‘crd.latitude’ and ‘crd.longitude’ variables are local, not global for use in other functions.
Second, the webscrptres extension permits acting on returned variables (and at that I’m not sure if the extension permits returning an object, i.e. both lat an lon variables) only after the JavaScript portion of the source file completes. In practical terms, even if I could pass an object to a TADS3 function after this code the browser would repeatedly ask the client for permission to share their location.
Another alternative I’ve thought of, of course, is simply including the GPS JavaScript code as stand-alone include file (via main.htm) and processing it that way. Looking at util.js and friends did not immediately reveal how I might do this.
Overall, either using webscrptres or not (though I confess that I like the elegance of using this method), how can I have TADS3 monitor geolocation objects and act to move the interactor to the appropriate location accordingly?