Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Thumbs.db
node_modules


src/electron/node_modules
src/electron/package-lock.json



Expand Down
22 changes: 22 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,26 @@ xmlns:android="http://schemas.android.com/apk/res/android"
<clobbers target="navigator.geolocation" />
</js-module>
</platform>

<!-- Electron -->
<platform name="electron">
<framework src="src/electron" />
<hook type="before_build" src="scripts/electron/startRebuild.js" />

<js-module src="www/geolocation.js" name="geolocation">
<clobbers target="navigator.geolocation" />
</js-module>

<js-module src="www/Coordinates.js" name="Coordinates">
<clobbers target="Coordinates" />
</js-module>

<js-module src="www/PositionError.js" name="PositionError">
<clobbers target="PositionError" />
</js-module>

<js-module src="www/Position.js" name="Position">
<clobbers target="Position" />
</js-module>
</platform>
</plugin>
20 changes: 20 additions & 0 deletions scripts/electron/startRebuild.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
);
});
};
59 changes: 59 additions & 0 deletions src/electron/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 26 additions & 0 deletions src/electron/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}