diff --git a/core/client.js b/core/client.js index 6f57ab88..f774427f 100644 --- a/core/client.js +++ b/core/client.js @@ -6,6 +6,7 @@ var gCredentials; var gErrorCount = 0; var gCommandHistory = []; var gSendKeyEvents = false; +var gGeolocatorWatch; var kMaxCommandHistory = 16; @@ -151,12 +152,50 @@ function receive(data) { window.removeEventListener("keyup", keyEvent); } gSendKeyEvents = value; + } else if (line && line[0] && line[0].action == "getCurrentPosition") { + navigator.geolocation.getCurrentPosition(geolocationPosition, geolocationError, line[0].options); + } else if (line && line[0] && line[0].action == "watchPosition") { + if (navigator && navigator.geolocation && gGeolocatorWatch === undefined) { + gGeolocatorWatch = navigator.geolocation.watchPosition(geolocationPosition, geolocationError, line[0].options); + } + } else if (line && line[0] && line[0].action == "clearWatch") { + if (navigator && navigator.geolocation && gGeolocatorWatch !== undefined) { + navigator.geolocation.clearWatch(gGeolocatorWatch); + } } else { print(document.getElementById(target), line); } } } +function geolocationPosition(position) { + send({ + event: 'geolocation', + position: { + timestamp: position.timestamp, + coords: { + latitude: position.coords.latitude, + longitude: position.coords.longitude, + altitude: position.coords.altitude, + accuracy: position.coords.accuracy, + altitudeAccuracy: position.coords.altitudeAccuracy, + heading: position.coords.heading, + speed: position.coords.speed, + }, + }, + }); +} + +function geolocationError(error) { + send({ + event: 'geolocation', + error: { + code: error.code, + message: error.message, + }, + }); +} + function keyEvent(event) { send({ event: "key", @@ -493,6 +532,10 @@ function connectSocket() { ['setTitle', 'value'], ['split', 'options'], ['setSendKeyEvents', 'value'], + + ['getCurrentPosition', 'options'], + ['watchPosition', 'options'], + ['clearWatch'], ], })); } diff --git a/packages/cory/geolocation/geolocation.js b/packages/cory/geolocation/geolocation.js new file mode 100644 index 00000000..8a715712 --- /dev/null +++ b/packages/cory/geolocation/geolocation.js @@ -0,0 +1,19 @@ +"use strict"; + +core.register("geolocation", function(event) { + terminal.print("hi"); + terminal.cork(); + if (event.position) { + for (let field in event.position.coords) { + let value = event.position.coords[field]; + terminal.print(field, ": ", value != null ? value.toString() : "null"); + } + } else if (event.error) { + terminal.print(event.error.message); + terminal.clearWatch(); + } + terminal.uncork(); +}); + +terminal.watchPosition({enableHighAccuracy: true}); +//terminal.watchPosition(); \ No newline at end of file