diff --git a/.gitignore b/.gitignore index 6964ea05..8c52b4cd 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ Thumbs.db node_modules +src/electron/node_modules +src/electron/package-lock.json diff --git a/plugin.xml b/plugin.xml index f294a7a2..dcc403e5 100644 --- a/plugin.xml +++ b/plugin.xml @@ -120,4 +120,26 @@ xmlns:android="http://schemas.android.com/apk/res/android" + + + + + + + + + + + + + + + + + + + + + + diff --git a/scripts/electron/startRebuild.js b/scripts/electron/startRebuild.js new file mode 100644 index 00000000..eaf2bb4f --- /dev/null +++ b/scripts/electron/startRebuild.js @@ -0,0 +1,20 @@ +const path = require('path'); +const exec = require('child_process').exec; +const packageName = require('../../package.json').name; + +module.exports = function () { + return new Promise(function (resolve, reject) { + console.log('Rebuilding packages for current electron version'); + exec('npm run rebuild', { cwd: path.join('plugins', packageName, 'src', 'electron') }, + function (error, stdout, stderr) { + if (error !== null) { + console.log('rebuild failed with message: ' + error.message); + reject(error); + } else { + console.log('rebuild ok'); + resolve(); + } + } + ); + }); +}; \ No newline at end of file diff --git a/src/electron/index.js b/src/electron/index.js new file mode 100644 index 00000000..af298d13 --- /dev/null +++ b/src/electron/index.js @@ -0,0 +1,59 @@ +let geolocation = null; +let locator = null; +if (/^win/i.test(process.platform)) { + geolocation = require('@nodert-win11-22h2/windows.devices.geolocation'); + locator = new geolocation.Geolocator(); +} + +module.exports = { + getLocation: ([options]) => { + options = parseParameters(options); + return new Promise((resolve, reject) => { + if (locator) { + locator.DesiredAccuracy = options.enableHighAccuracy ? geolocation.PositionAccuracy.high : geolocation.PositionAccuracy.default; + locator.getGeopositionAsync((error, geocoord) => { + const position = {}; + if (error) { + reject(error); + return; + } + position.accuracy = geocoord.coordinate.accuracy; + position.altitudeAccuracy = geocoord.coordinate.altitudeAccuracy; + position.heading = geocoord.coordinate.heading; + position.altitude = geocoord.coordinate.altitude; + position.latitude = geocoord.coordinate.latitude; + position.longitude = geocoord.coordinate.longitude; + resolve(position); + }); + } else { + reject(new Error('Not Supported')); + } + }); + } +}; + +function parseParameters (options) { + var opt = { + maximumAge: 0, + enableHighAccuracy: false, + timeout: Infinity + }; + + if (options) { + if (options.maximumAge !== undefined && !isNaN(options.maximumAge) && options.maximumAge > 0) { + opt.maximumAge = options.maximumAge; + } + if (options.enableHighAccuracy !== undefined) { + opt.enableHighAccuracy = options.enableHighAccuracy; + } + if (options.timeout !== undefined && !isNaN(options.timeout)) { + if (options.timeout < 0) { + opt.timeout = 0; + } else { + opt.timeout = options.timeout; + } + } + } + + return opt; +} \ No newline at end of file diff --git a/src/electron/package.json b/src/electron/package.json new file mode 100644 index 00000000..c70aaf8d --- /dev/null +++ b/src/electron/package.json @@ -0,0 +1,26 @@ +{ + "name": "cordova-plugin-geolocation-electron", + "version": "1.0.0", + "description": "Electron Native Support for Cordova Geolocation Plugin", + "main": "index.js", + "keywords": [ + "cordova", + "electron", + "sqlite", + "native" + ], + "author": "Apache Software Foundation", + "license": "Apache-2.0", + "dependencies": { + "@nodert-win11-22h2/windows.devices.geolocation": "^0.1.6" + }, + "devDependencies": { + "electron-rebuild": "^3.2.9" + }, + "scripts": { + "rebuild": "npm i && electron-rebuild" + }, + "cordova": { + "serviceName": "Geolocation" + } +}