Skip to content

Location

Read the device's geographic location from a script through environment.location. Coarse reads ride the base location grant; precise accuracy is a separate, explicit opt-in.

Request access and read a fix

Ask for access once, then read the current location. Reads never prompt on their own – call requestAccess() first.

javascript
await environment.location.requestAccess();
var here = await environment.location.current();
console.log(here.latitude, here.longitude, '±' + here.horizontalAccuracy + 'm');

current() resolves a GeoCoordinate. On a live fix it also carries horizontalAccuracy (metres) and timestamp; those are null on a coordinate you construct yourself.

Coordinates: distance and bearing

GeoCoordinate carries the great-circle helpers, so you can measure against a point of interest without any external maths:

javascript
var target = new GeoCoordinate(48.1372, 11.5756);   // latitude, longitude
console.log('distance', Math.round(here.distanceTo(target)), 'm');   // metres
console.log('bearing', Math.round(here.bearingTo(target)) + '°');    // 0–360 from north

Watch position

Observe location continuously – for example, reveal something when the user gets close:

javascript
var target = new GeoCoordinate(48.1372, 11.5756);
var sub = environment.location.watch(function(here) {
    if (here.distanceTo(target) < 50) {
        sub.stop();
        scene.setVariable('arrived', true);
    }
});

Call sub.stop() to end the subscription.

Precise accuracy

The base grant may be coarse. When your experience genuinely needs precise positioning, ask for it explicitly – the user sees a confirmation carrying your reason, then the system accuracy prompt:

javascript
var precise = await environment.location.requestPreciseAccess({
    reason: 'to guide you to nearby points of interest'
});
if (precise) {
    var fix = await environment.location.current();
}

Ask only when needed

Request precise accuracy at the moment the feature needs it, not on load – the prompt reads better in context, and coarse location is enough for most distance/region checks.