Geolocation

Get the device's GPS position once or stream live location updates.

Get started

Readings are delivered on the location event, so subscribe before you call. Request the location permission first.

import { createBdkNative } from "@bdk/native/browser";

const bdk = createBdkNative();

// Subscribe first — the position is delivered to this event, not the promise.
bdk.on("location", (info) => {
  console.log("device location", info);
});

await bdk.permissions.ask("location");

Get the current position

Get a single GPS fix. The reading arrives on the location event.

bdk.on("location", (info) => {
  console.log("current position", info);
});

await bdk.location.getCurrentPosition();

Stream location while the app is open

Start a continuous stream of foreground updates. Each update fires the location event. Call stopForegroundTracking() to end it.

const off = bdk.on("location", (info) => {
  console.log("moving", info);
});

await bdk.location.startForegroundTracking();

// later, to stop receiving updates in your app code:
off();

Stop a foreground stream

Stop an active foreground stream.

await bdk.location.stopForegroundTracking();

Keep tracking in the background

Keep receiving location updates while the app is backgrounded. The result fires the backgroundLocationEnabled event with { enabled, alreadyRunning, reason }.

Android only; elsewhere it doesn't run. Check backgroundLocationEnabled (especially reason) to know whether it actually started.

bdk.on("backgroundLocationEnabled", ({ enabled, alreadyRunning, reason }) => {
  console.log({ enabled, alreadyRunning, reason });
});

// options are required and forwarded to the native command.
await bdk.location.enableBackground({ interval: 60000 });

Stop background tracking

Stop background updates. The result fires the backgroundLocationDisabled event with { enabled }. Android only.

bdk.on("backgroundLocationDisabled", ({ enabled }) => {
  console.log("background location enabled?", enabled);
});

await bdk.location.disableBackground();

Events

All position data arrives on events. bdk.on(...) returns an unsubscribe function.

  • location — every position reading (one-shot or streamed).
  • backgroundLocationEnabled{ enabled, alreadyRunning, reason }.
  • backgroundLocationDisabled{ enabled }.
const offs = [
  bdk.on("location", (info) => console.log("location", info)),
  bdk.on("backgroundLocationEnabled", (p) => console.log("bg on", p)),
  bdk.on("backgroundLocationDisabled", (p) => console.log("bg off", p))
];

// Clean up all listeners at once.
offs.forEach((off) => off());