π LocationΒΆ
Get the deviceβs current GPS position.
const position = await app.hardware.location();
console.log(position.latitude); // e.g. 51.5074
console.log(position.longitude); // e.g. -0.1278
console.log(position.accuracy); // metres of uncertainty
console.log(position.altitude); // metres above sea level, or null
console.log(position.timestamp); // Unix timestamp in ms
βοΈ OptionsΒΆ
Option |
Type |
Default |
Description |
|---|---|---|---|
|
boolean |
|
Request GPS-level accuracy. Slower and uses more battery than network-based location. |
|
number |
|
Max milliseconds to wait before rejecting. |
|
number |
|
Max age in ms of a cached position to accept. |
π Browser behaviourΒΆ
Uses the Geolocation API. The browser will prompt the user for permission before returning a position. Requires a secure context (HTTPS or localhost).
If the user denies permission or the device does not support geolocation, the Promise rejects with an error message.
π‘ Common useΒΆ
async function findNearby(context) {
try {
const position = await app.hardware.location({ highAccuracy: true });
const { latitude, longitude } = position;
await loadNearbyResults(latitude, longitude, context);
} catch (error) {
context.render("#locationStatus", "Location unavailable. Please enable location access.");
}
}
Show a clear message when location fails. The user may have denied permission or have no signal. Tell them what to do rather than showing a generic error.