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.

// 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);
});

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);

Hierarchy (View Summary, Expand)

Constructors

Properties

The accuracy of the provided position.

onNorthAlignedOrientationUpdate: event1<quat, void>

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

Methods

  • Exposes User Data

    Retrieves the device's current location. 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.

    Parameters

    • onSucess: (geoPosition: GeoPosition) => void
    • onError: (error: string) => void

    Returns void

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

    Parameters

    • type: string

    Returns boolean

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

    Parameters

    Returns boolean

MMNEPVFCICPMFPCPTTAAATR