Skip to main content

LocationService

Class

The LocationService allows the user to provide their location to Lens applications if they so desire.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

The LocationService allows the user to provide their location to Lens applications if they so desire. For privacy reasons, the user is asked for permission to report location information.

Spectacles: To use location services, users must be logged in and paired with their Snapchat account, and their location permission must be enabled. Users are also expected to be connected to the internet. To access the location API on Spectacles, refer to the Spectacles Location documentation for examples and extra setup instructions regarding permissions.

Index

Example

// Instantiate LocationService and obtain regular location and heading updates
let locationService = undefined;
let timestampLastLocation = undefined;

const repeatUpdateUserLocation = script.createEvent('DelayedCallbackEvent');

function createAndLogLocationAndHeading() {

//Create location handler
locationService = GeoLocation.createLocationService();

//Set the accuracy
locationService.accuracy = GeoLocationAccuracy.Navigation;

// Acquire heading orientation updates
var onOrientationUpdate = function (northAlignedOrientation) {
//Providing 3DoF north aligned rotation in quaternion form
let heading = GeoLocation.getNorthAlignedHeading(northAlignedOrientation);
print("Heading orientation: " + heading.toFixed(3))
// Convert to a 2DoF rotation for plane rendering purposes
var rotation = heading * Math.PI / 180;
script.screenTransform.rotation = (quat.fromEulerAngles(0, 0, rotation));
};
locationService.onNorthAlignedOrientationUpdate.add(onOrientationUpdate);

//Acquire next location immediately with zero delay
repeatUpdateUserLocation.reset(0.0);
}

script.createEvent('OnStartEvent').bind(() => {
createAndLogLocationAndHeading();
});

repeatUpdateUserLocation.bind(() => {
//Get users location
locationService.getCurrentPosition(
function (geoPosition) {
//Check if location coordinates have been updated based on timestamp
let geoPositionTimestampMs = geoPosition.timestamp.getTime();
if (timestampLastLocation != geoPositionTimestampMs) {
script.latitude = geoPosition.latitude;
script.longitude = geoPosition.longitude;
if (geoPosition.altitude != 0) {
script.altitude = geoPosition.altitude;
}
script.horizontalAccuracy = geoPosition.horizontalAccuracy;
script.verticalAccuracy = geoPosition.vertical;
print('location source: ' + geoPosition.locationSource);
timestampLastLocation = geoPositionTimestampMs;
}
},
function (error) {
print(error);
}
);
//Acquire next location update in 1 second
repeatUpdateUserLocation.reset(1.0);
});

Examples for Visualising location coordinates in a 2D Map

For mobile platforms use Map Component. For Spectacles, follow the Location Guide section regarding location and heading visualization. If you would like to create your own version for a custom 2D Map Visualization, use the below example as a reference which utilizes Map Module.

// Example to create a custom 2D Map Visualization using Map Module
//@input Asset.MapModule mapModule

// Create location handler
var locationService = GeoLocation.createLocationService();

// Set the accuracy
locationService.accuracy = GeoLocationAccuracy.Navigation;

// Get users location.
locationService.getCurrentPosition(
function(geoPosition) {
global.lat = geoPosition.latitude;
global.lng = geoPosition.longitude;
},
function(error) {
print(error);
}
);

// Setup MapTextureProvider
var location = LocationAsset
.getGeoAnchoredPosition(global.lng, global.lat)
.location.adjacentTile(0,0,-3);
var provider = script.mapModule.createMapTextureProvider();
provider.control.location = location;
script.mesh.mainPass.baseTex = provider;

//Get the postion of the marker
var xy = script.mapModule.longLatToImageRatio(global.lng, global.lat, global.mapProvider.control.location);

Extends

Properties

accuracy

accuracy: GeoLocationAccuracy

The desired accuracy of the provided position.

Since

Lens Scripting Version 228


onNorthAlignedOrientationUpdate

readonly onNorthAlignedOrientationUpdate: event2<quat, Accuracy, void>

Event to notify when north aligned orientation data is available to use. Use in combination with GeoLocation.getNorthAlignedHeading to acquire heading direction. Arguments:

  • northAlignedOrientation: The north-aligned orientation of the device as a quaternion
  • headingAccuracy: The accuracy of the heading, represented by the CompassTrackingData.Accuracy enum.

Since

Lens Scripting Version 233

Methods

getCurrentPosition()

getCurrentPosition(onSucess, onError): void

Exposes user data

Retrieves the device's current location.

Parameters

ParameterType
onSucess(geoPosition) => void
onError(error) => void

Returns

void

Remarks

  • onSuccess: a callback function that takes a GeoPosition object as its sole input parameter.
  • onError: a callback function that takes a string error message as its sole input parameter.

Since

Lens Scripting Version 228


getTypeName()

static getTypeName(): TypeName<LocationService>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<LocationService>

Overrides

ScriptObject.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

ScriptObject.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

ScriptObject.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

ScriptObject.isSame