Skip to content

Instantly share code, notes, and snippets.

@lantica
Last active February 22, 2022 08:54
Show Gist options
  • Save lantica/0089a0e6ce731aae7944bed448685e35 to your computer and use it in GitHub Desktop.
Save lantica/0089a0e6ce731aae7944bed448685e35 to your computer and use it in GitHub Desktop.
Get location from geoclue
(async()=>{
const dbus = require("dbus-next");
const bus = dbus.systemBus();
const obj = await bus.getProxyObject("org.freedesktop.GeoClue2", "/org/freedesktop/GeoClue2/Manager");
const iFace = obj.getInterface("org.freedesktop.GeoClue2.Manager");
const clientPath = await iFace.GetClient();
const clientObj = await bus.getProxyObject("org.freedesktop.GeoClue2", clientPath);
const clientIface = clientObj.getInterface("org.freedesktop.GeoClue2.Client");
const prop = clientObj.getInterface("org.freedesktop.DBus.Properties");
await prop.Set("org.freedesktop.GeoClue2.Client", "DesktopId", new dbus.Variant("s", "test"));
clientIface.on("LocationUpdated", async (_, locPath) => {
console.log(locPath);
if (locPath === "/") return;
const locObj = await bus.getProxyObject("org.freedesktop.GeoClue2", locPath);
const locProp = locObj.getInterface("org.freedesktop.DBus.Properties");
const [long, lat, acc, alt, spd, head, desc, time] = await Promise.allSettled(
[
"Longitude",
"Latitude",
"Accuracy",
"Altitude",
"Speed",
"Heading",
"Description",
"Timestamp",
].map(prop => locProp.Get("org.freedesktop.GeoClue2.Location",prop))
);
console.log(long, lat, acc, alt, spd, head, desc, time);
});
await clientIface.Start();
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment