Request a position for nearby results, follow movement on an active screen, or register background updates for a feature that needs them.

Declare location permission

ts
// kuito.config.ts
import type { KuitoConfig } from 'kuito/config';

export default {
  name: 'Screenings',
  ios: { bundleIdentifier: 'com.example.screenings' },
  android: { applicationId: 'com.example.screenings' },
  permissions: { location: 'Find screenings near you.' },
} satisfies KuitoConfig;

Request a current position

tsx
import { Text, location, permissions } from 'kuito';

const { status } = await permissions.request('location');

if (status === 'granted') {
  const here = await location.current({ accuracy: 'high' });
  const [place] = await location.reverseGeocode(here.coords);
}

function Here() {
  // Locates while Here is mounted; the fixes land in location.position.
  location.watch({ distanceInterval: 20 });

  return <Text>{`${location.position.value?.coords.latitude ?? '…'}`}</Text>;
}

Follow movement

Set distanceInterval in meters. Android also accepts timeInterval in milliseconds; iOS delivers fixes as they arrive. Create location.watch in component setup for automatic cleanup, or keep its subscription and call remove() outside a component. Render location.error when a watch fails.

location.watchHeading returns a promise of a subscription. Stop it on cleanup, including when it resolves after the screen has already unmounted; the API example shows that race explicitly.

Configure background location

ts
// kuito.config.ts
import type { KuitoConfig } from 'kuito/config';

export default {
  name: 'Runs',
  ios: { bundleIdentifier: 'com.example.runs' },
  android: { applicationId: 'com.example.runs' },
  permissions: {
    location: 'Map your runs.',
    locationAlways: 'Record your runs with the screen off.',
  },
} satisfies KuitoConfig;

Register background updates

tsx
import { location, permissions, storage } from 'kuito';

// At module scope: a background launch runs the app's modules, not its UI.
location.defineUpdates('run', async ({ locations }) => {
  const track: unknown[] = JSON.parse((await storage.get('track')) ?? '[]');

  await storage.set('track', JSON.stringify([...track, ...locations.map((fix) => fix.coords)]));
});

async function startRun() {
  const { status } = await permissions.request('locationAlways');

  if (status === 'granted')
    await location.startUpdates('run', {
      accuracy: 'high',
      distanceInterval: 10,
      notification: { title: 'Recording your run' },
    });
}

Behavior and limits

Request permission at the point of use and handle denial. A cached last position can be null or too old for your task. Choose accuracy and distance intervals that fit the feature and battery budget. position and error expose the current watched state. Remove watchers outside component scope. Background updates require both foreground and background declarations and OS permission; they are not guaranteed continuous execution. Geocoding converts an address to coordinates; reverse geocoding does the opposite.

Continue

Read Permissions for the related workflow. The API reference lists every method, option, event and return type.

Referência

Exportações 26

Métodos, constantes e tipos expostos por este namespace nativo.

functioncurrentOne fresh fix.functionlastThe fix the platform last had, at once; null without one that meets options.functionwatchStarts locating into position.functionwatchHeadingCalls callback as the device turns, until remove().functiongeocodeThe positions of an address, as the platform's geocoder finds them.functionreverseGeocodeThe places at a position, nearest first.functiondefineUpdatesThe task that gets the fixes of startUpdates(name), as a background task (docs/background-tasks.md).functionstartUpdatesLocates in the background too, delivering fixes to the task name until stopUpdates(name), across the app's restarts.typeLocationAccuracyHow precise a fix should be; finer costs more power.typeLocationCoordsA fix's position.typeLocationObjectOne position fix of the device, with when it was taken and whether a mock location supplied it.typeLocationOptionsTrades a fix's accuracy against the battery it costs.typeLastKnownPositionOptionsSays how old and how coarse a cached fix location.last() may return before it resolves null.typeWatchPositionOptionsTrades how often and how precisely a position watch reports against the battery it costs.typeLocationHeadingWhich way the device points, for a compass or for turning a map with the user.typeGeocodedLocationA position location.geocode() found for an address, ready to center a map or measure a distance.typeGeocodedAddressOne place at a position.typeLocationErrorWhy a location call or watch failed, for telling the user or falling back.typeLocationTaskOptionsHow a location task's updates run.typeLocationTaskRunWhat a location task receives: the fixes since its last run, oldest first.typeLocationTaskResolve when the fixes are handled; throw or reject to report a failed run.valuepositionThe newest fix any watch brought; null until the first.valueerrorThe error that ended a watch, such as access revoked in Settings; null while none has.functionservicesEnabledLocation services are on for the device (not this app's permission).functionstopUpdatesStops the task's updates; the platform forgets them.functionupdatingWhether updates for the task are running, or will resume when its app launches.
Gerado a partir das declarações TypeScript públicas: assinaturas, props e predefinições vêm da fonte verificada, as descrições da sua JSDoc. Nenhuma predefinição é inventada.