Skip to main content
Version: 12 Dec 2024

Spatial Anchor

Module: Magic Leap Spaces

APIs for managing Spatial Anchors. More...

Classes

Name
structMLSpatialAnchorLocalizationInfo
A structure containing information about the device's localization state.
structMLSpatialAnchor
A structure representing a user-defined Spatial Anchor.
structMLSpatialAnchorCreateInfo
A structure used to populate anchor creation info when creating a new Spatial Anchor.
structMLSpatialAnchorQueryFilter
A collection of filters for Spatial Anchor queries.

Types

Name
typedef struct MLSpatialAnchorLocalizationInfoMLSpatialAnchorLocalizationInfo
A structure containing information about the device's localization state.
typedef struct MLSpatialAnchorMLSpatialAnchor
A structure representing a user-defined Spatial Anchor.
typedef struct MLSpatialAnchorCreateInfoMLSpatialAnchorCreateInfo
A structure used to populate anchor creation info when creating a new Spatial Anchor.
typedef struct MLSpatialAnchorQueryFilterMLSpatialAnchorQueryFilter
A collection of filters for Spatial Anchor queries.

Enums

Name
enumAnonymous Enum 29
{
MLResultAPIPrefix_SpatialAnchor = ( 0x439 << 16),
MLSpatialAnchor_MaxSpaceNameLength = 64
}
enumMLSpatialAnchorResult
{
MLSpatialAnchorResult_MaxAnchorLimitReached = MLResultAPIPrefix_SpatialAnchor,
MLSpatialAnchorResult_MinDistanceThresholdExceeded,
MLSpatialAnchorResult_InsufficientMapping,
MLSpatialAnchorResult_InvalidId,
MLSpatialAnchorResult_InvalidExpirationTimestamp,
MLSpatialAnchorResult_NotLocalized,
MLSpatialAnchorResult_ServerError,
MLSpatialAnchorResult_ServiceUnavailable,
MLSpatialAnchorResult_Ensure32Bits = 0x7FFFFFFF
}
SpatialAnchor-specific return codes.
enumMLSpatialAnchorLocalizationStatus
{
MLSpatialAnchorLocalizationStatus_NotLocalized,
MLSpatialAnchorLocalizationStatus_Localized,
MLSpatialAnchorLocalizationStatus_LocalizationPending,
MLSpatialAnchorLocalizationStatus_Ensure32Bits = 0x7FFFFFFF
}
The current localization status.
enumMLSpatialAnchorMappingMode
{
MLSpatialAnchorMappingMode_OnDevice,
MLSpatialAnchorMappingMode_ARCloud,
MLSpatialAnchorMappingMode_Ensure32Bits = 0x7FFFFFFF
}
Mapping mode.
enumMLSpatialAnchorQuality
{
MLSpatialAnchorQuality_Low,
MLSpatialAnchorQuality_Medium,
MLSpatialAnchorQuality_High,
MLSpatialAnchorQuality_Ensure32Bits = 0x7FFFFFFF
}
The quality of the local space around the anchor.

Functions

Name
voidMLSpatialAnchorLocalizationInfoInit(MLSpatialAnchorLocalizationInfo * inout_info)
Initialize default values for MLSpatialAnchorLocalizationInfo.
voidMLSpatialAnchorInit(MLSpatialAnchor * inout_anchor)
Initialize default values for a MLSpatialAnchor structure.
voidMLSpatialAnchorCreateInfoInit(MLSpatialAnchorCreateInfo * inout_info)
Initialize default values for a MLSpatialAnchorCreateInfo structure.
voidMLSpatialAnchorQueryFilterInit(MLSpatialAnchorQueryFilter * inout_query_filter)
Initializes the default values for a query filter.
MLResultMLSpatialAnchorTrackerCreate(MLHandle * out_handle)
Create a Spatial Anchor tracker.
MLResultMLSpatialAnchorTrackerDestroy(MLHandle handle)
Destroy a previously created Spatial Anchor tracker.
MLResultMLSpatialAnchorCreate(MLHandle handle, const MLSpatialAnchorCreateInfo create_info, MLSpatialAnchor out_anchor)
Create a new local Spatial Anchor at the desired location.
MLResultMLSpatialAnchorPublish(MLHandle handle, MLUUID anchor_id)
Publish an existing local Spatial Anchor to the persistent backend.
MLResultMLSpatialAnchorDelete(MLHandle handle, MLUUID anchor_id)
Delete an existing Spatial Anchor.
MLResultMLSpatialAnchorUpdate(MLHandle handle, const MLSpatialAnchor * anchor)
Update a Spatial Anchor's properties.
MLResultMLSpatialAnchorQueryCreate(MLHandle handle, const MLSpatialAnchorQueryFilter query_filter, MLHandle out_query_handle, uint32_t * out_results_count)
Create a new query for Spatial Anchors in the current space.
MLResultMLSpatialAnchorQueryDestroy(MLHandle handle, MLHandle query_handle)
Destroy a previously created query handle and release its associated resources.
MLResultMLSpatialAnchorQueryGetResult(MLHandle handle, MLHandle query_handle, uint32_t first_index, uint32_t last_index, MLSpatialAnchor * out_results)
Get the result of a previous Spatial Anchor query.
const char *MLSpatialAnchorGetResultString(MLResult result)
Returns an ASCII string for MLSpatialAnchorResult and MLResult codes.
MLResultMLSpatialAnchorGetLocalizationInfo(MLHandle handle, MLSpatialAnchorLocalizationInfo * out_localization_info)
Get the current localization status of the device.

Detailed Description

APIs for managing Spatial Anchors.

Spatial Anchor management is closely tied to the selected mapping mode on the device. The modes are mutually exclusive and affect the functionality of these APIs. The available mapping modes are:

On-Device Mode - A persistent mode in which anchors are persisted locally and will be available in multiple sessions when localized to the same space in which they were published.

AR Cloud Mode - A persistent mode in which anchors are persisted in the cloud environment and will be available in multiple sessions to devices that are localized to the same space in which they were published.

Shared Object:

  • perception.magicleap*

Enums Documentation

Anonymous Enum 29

EnumeratorValueDescription
MLResultAPIPrefix_SpatialAnchor( 0x439 << 16)Defines the prefix for MLSpatialAnchorResult codes.
MLSpatialAnchor_MaxSpaceNameLength64Maximum size for the name of the space in the MLSpatialAnchorLocalizationInfo structure.

MLSpatialAnchorResult

EnumeratorValueDescription
MLSpatialAnchorResult_MaxAnchorLimitReachedMLResultAPIPrefix_SpatialAnchorThe maximum number of anchors for the current space has been reached.
MLSpatialAnchorResult_MinDistanceThresholdExceededThe minimum distance between anchors was not met.
MLSpatialAnchorResult_InsufficientMappingThe space has not been sufficiently mapped to allow this operation.
MLSpatialAnchorResult_InvalidIdThe provided anchor Id was not valid.
MLSpatialAnchorResult_InvalidExpirationTimestampThe provided expiration suggestion was not valid.
MLSpatialAnchorResult_NotLocalizedThe operation cannot be completed because the device has not yet localized.
MLSpatialAnchorResult_ServerErrorThere was an error communicating with the server.
MLSpatialAnchorResult_ServiceUnavailableThe operation failed because the underlying service is not yet available, retry later.
MLSpatialAnchorResult_Ensure32Bits0x7FFFFFFFEnsure enum is represented as 32 bits.

SpatialAnchor-specific return codes.

API Level:

  • 20

MLSpatialAnchorLocalizationStatus

EnumeratorValueDescription
MLSpatialAnchorLocalizationStatus_NotLocalizedThe device is not currently localized.
MLSpatialAnchorLocalizationStatus_LocalizedThe device has localized successfully.
MLSpatialAnchorLocalizationStatus_LocalizationPendingA localization attempt is currently in progress.
MLSpatialAnchorLocalizationStatus_Ensure32Bits0x7FFFFFFFEnsure enum is represented as 32 bits.

The current localization status.

Deprecated

Deprecated since 1.3.0. Scheduled for removal. Replaced by MLSpaceLocalizationStatus.

API Level:

  • 20

MLSpatialAnchorMappingMode

EnumeratorValueDescription
MLSpatialAnchorMappingMode_OnDeviceUsing on-device mapping.
MLSpatialAnchorMappingMode_ARCloudUsing cloud-based mapping.
MLSpatialAnchorMappingMode_Ensure32Bits0x7FFFFFFFEnsure enum is represented as 32 bits.

Mapping mode.

Deprecated

Deprecated since 1.3.0. Scheduled for removal.

API Level:

  • 20

MLSpatialAnchorQuality

EnumeratorValueDescription
MLSpatialAnchorQuality_LowLow quality, this anchor can be expected to move significantly.
MLSpatialAnchorQuality_MediumMedium quality, this anchor may move slightly.
MLSpatialAnchorQuality_HighHigh quality, this anchor is stable and suitable for digital content attachment.
MLSpatialAnchorQuality_Ensure32Bits0x7FFFFFFFEnsure enum is represented as 32 bits.

The quality of the local space around the anchor.

This can change over time as the user continues to scan the space.

API Level:

  • 31

Types Documentation

MLSpatialAnchorLocalizationInfo

typedef struct MLSpatialAnchorLocalizationInfo MLSpatialAnchorLocalizationInfo;

A structure containing information about the device's localization state.

More Info

Deprecated

Deprecated since 1.3.0. Scheduled for removal. Replaced by MLSpaceLocalizationResult.

API Level:

  • 20

MLSpatialAnchor

typedef struct MLSpatialAnchor MLSpatialAnchor;

A structure representing a user-defined Spatial Anchor.

More Info

API Level:

  • 31

MLSpatialAnchorCreateInfo

typedef struct MLSpatialAnchorCreateInfo MLSpatialAnchorCreateInfo;

A structure used to populate anchor creation info when creating a new Spatial Anchor.

More Info

API Level:

  • 20

MLSpatialAnchorQueryFilter

typedef struct MLSpatialAnchorQueryFilter MLSpatialAnchorQueryFilter;

A collection of filters for Spatial Anchor queries.

Filters that have been set will be combined via logical conjunction. E.g. results must match the ids filter AND fall within the radius constraint when both have been set.

This struct must be initialized by calling MLSpatialAnchorQueryFilterInit before use.

More Info

API Level:

  • 20

Functions Documentation

MLSpatialAnchorLocalizationInfoInit

static inline void MLSpatialAnchorLocalizationInfoInit(
MLSpatialAnchorLocalizationInfo * inout_info
)

Initialize default values for MLSpatialAnchorLocalizationInfo.

Parameters

MLSpatialAnchorLocalizationInfo *inout_infoThe localization info to initialize.

Required Permissions:

  • None
Deprecated

Deprecated since 1.3.0. Scheduled for removal. Replaced by MLSpaceLocalizationResultInit.

API Level:

  • 20

MLSpatialAnchorInit

static inline void MLSpatialAnchorInit(
MLSpatialAnchor * inout_anchor
)

Initialize default values for a MLSpatialAnchor structure.

Parameters

MLSpatialAnchor *inout_anchorThe anchor to initialize.

Required Permissions:

  • None

API Level:

  • 31

MLSpatialAnchorCreateInfoInit

static inline void MLSpatialAnchorCreateInfoInit(
MLSpatialAnchorCreateInfo * inout_info
)

Initialize default values for a MLSpatialAnchorCreateInfo structure.

Parameters

MLSpatialAnchorCreateInfo *inout_infoThe info struct to initialize.

Required Permissions:

  • None

API Level:

  • 20

MLSpatialAnchorQueryFilterInit

static inline void MLSpatialAnchorQueryFilterInit(
MLSpatialAnchorQueryFilter * inout_query_filter
)

Initializes the default values for a query filter.

Parameters

MLSpatialAnchorQueryFilter *inout_query_filterThe filter to initialize.

Required Permissions:

  • None

API Level:

  • 20

MLSpatialAnchorTrackerCreate

MLResult MLSpatialAnchorTrackerCreate(
MLHandle * out_handle
)

Create a Spatial Anchor tracker.

Parameters

MLHandle *out_handleA pointer to an MLHandle which will contain a handle for a Spatial Anchor tracker if successful, otherwise it will be ML_INVALID_HANDLE.

Returns

MLResultMLResult_InvalidParamThe out_handle parameter was not valid.
MLResultMLResult_OkSuccessfully created the tracker.
MLResultMLResult_PermissionDeniedNecessary permission is missing.
MLResultMLResult_UnspecifiedFailureThe tracker was not created.

Required Permissions:

  • com.magicleap.permission.SPATIAL_ANCHOR (protection level: normal)

API Level:

  • 20

MLSpatialAnchorTrackerDestroy

MLResult MLSpatialAnchorTrackerDestroy(
MLHandle handle
)

Destroy a previously created Spatial Anchor tracker.

Parameters

MLHandlehandleA handle to a Spatial Anchor tracker created by MLSpatialAnchorCreate().

Returns

MLResultMLResult_InvalidParamThe handle parameter was not valid.
MLResultMLResult_OkSuccessfully destroyed the tracker.
MLResultMLResult_UnspecifiedFailureThe tracker was not destroyed.

Required Permissions:

  • None

API Level:

  • 20

MLSpatialAnchorCreate

MLResult MLSpatialAnchorCreate(
MLHandle handle,
const MLSpatialAnchorCreateInfo * create_info,
MLSpatialAnchor * out_anchor
)

Create a new local Spatial Anchor at the desired location.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
const MLSpatialAnchorCreateInfo *create_infoA struct containing the creation info for the new anchor.
MLSpatialAnchor *out_anchorThe created anchor.

Returns

MLResultMLResult_InvalidParamOne of the parameters was null.
MLResultMLResult_OkThe anchor was successfully created.
MLResultMLSpatialAnchorResult_InsufficientMappingInsufficient mapping.
MLResultMLSpatialAnchorResult_InvalidExpirationTimestampInvalid expiration timestamp.
MLResultMLSpatialAnchorResult_MaxAnchorLimitReachedMaximum anchor limit reached.
MLResultMLSpatialAnchorResult_MinDistanceThresholdExceededMinimum distance threshold exceeded.

Required Permissions:

  • None

On success, out_anchor will be returned with a newly generated ID.

Any unpublished anchor will be lost if the Headpose session is lost. See MLHeadTrackingGetMapEvents for more details.

API Level:

  • 20

MLSpatialAnchorPublish

MLResult MLSpatialAnchorPublish(
MLHandle handle,
MLUUID anchor_id
)

Publish an existing local Spatial Anchor to the persistent backend.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
MLUUIDanchor_idThe Id of the local anchor to publish.

Returns

MLResultMLResult_InvalidParamOne of the parameters was null.
MLResultMLResult_OkThe anchor was successfully published and persisted.
MLResultMLResult_UnauthenticatedInvalid authentication credentials for this operation.
MLResultMLSpatialAnchorResult_NotLocalizedNot localized.
MLResultMLSpatialAnchorResult_InvalidIdInvalid Id provided.
MLResultMLSpatialAnchorResult_MaxAnchorLimitReachedMaximum anchor limit reached.
MLResultMLSpatialAnchorResult_MinDistanceThresholdExceededMinimum distance threshold exceeded.
MLResultMLSpatialAnchorResult_ServerErrorServer error occurred.

Required Permissions:

  • None

Depending on the currently selected mapping mode, this can store the anchor locally or in the cloud. This call will fail if the device is not localized to a space.

Any unpublished anchor will be lost if the Headpose session is lost. See MLHeadTrackingGetMapEvents for more details.

API Level:

  • 20

MLSpatialAnchorDelete

MLResult MLSpatialAnchorDelete(
MLHandle handle,
MLUUID anchor_id
)

Delete an existing Spatial Anchor.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
MLUUIDanchor_idThe Id of the anchor to delete.

Returns

MLResultMLResult_InvalidParamOne of the parameters was null.
MLResultMLResult_OkThe anchor was successfully deleted.
MLResultMLResult_UnauthenticatedInvalid authentication credentials for this operation.
MLResultMLSpatialAnchorResult_InvalidIdInvalid Id provided.
MLResultMLSpatialAnchorResult_ServerErrorServer error occurred.

Required Permissions:

  • None

If successful, this will delete the anchor from persistent storage based on the currently selected mapping mode.

API Level:

  • 20

MLSpatialAnchorUpdate

MLResult MLSpatialAnchorUpdate(
MLHandle handle,
const MLSpatialAnchor * anchor
)

Update a Spatial Anchor's properties.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
const MLSpatialAnchor *anchorThe anchor containing the desired updated properties.

Returns

MLResultMLResult_InvalidParamOne of the parameters was null.
MLResultMLResult_OkThe anchor was successfully updated.
MLResultMLResult_UnauthenticatedInvalid authentication credentials for this operation.
MLResultMLSpatialAnchorResult_InvalidIdInvalid Id provided.
MLResultMLSpatialAnchorResult_InvalidExpirationTimestampInvalid expiration timestamp.
MLResultMLSpatialAnchorResult_ServerErrorServer error occurred.

Required Permissions:

  • None

The only property that can currently be updated is the expiration timestamp.

API Level:

  • 20

MLSpatialAnchorQueryCreate

MLResult MLSpatialAnchorQueryCreate(
MLHandle handle,
const MLSpatialAnchorQueryFilter * query_filter,
MLHandle * out_query_handle,
uint32_t * out_results_count
)

Create a new query for Spatial Anchors in the current space.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
const MLSpatialAnchorQueryFilter *query_filterThe filter structure for the query.
MLHandle *out_query_handleThe handle for this query to be used with MLSpatialAnchorQueryGetResult.
uint32_t *out_results_countThe total number of entries found by the query. This can be any number between 0 and max_results.

Returns

MLResultMLResult_InvalidParamOne of the parameters was null.
MLResultMLResult_OkThe query executed successfully.
MLResultMLResult_UnspecifiedFailureUnspecified error occurred.
MLResultMLSpatialAnchorResult_NotLocalizedNot localized to a space.

Required Permissions:

  • None

It is the responsibility of the caller to call MLSpatialAnchorQueryDestroy with the query handle returned from this function after the results are no longer needed.

API Level:

  • 20

MLSpatialAnchorQueryDestroy

MLResult MLSpatialAnchorQueryDestroy(
MLHandle handle,
MLHandle query_handle
)

Destroy a previously created query handle and release its associated resources.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
MLHandlequery_handleHandle to a Spatial Anchor query.

Returns

MLResultMLResult_InvalidParamOne of the parameters was null.
MLResultMLResult_OkThe handle was successfully destroyed.

Required Permissions:

  • None

API Level:

  • 20

MLSpatialAnchorQueryGetResult

MLResult MLSpatialAnchorQueryGetResult(
MLHandle handle,
MLHandle query_handle,
uint32_t first_index,
uint32_t last_index,
MLSpatialAnchor * out_results
)

Get the result of a previous Spatial Anchor query.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
MLHandlequery_handleHandle to a query obtained from MLSpatialAnchorQuery.
uint32_tfirst_indexThe first index.
uint32_tlast_indexThe last index.
MLSpatialAnchor *out_resultsAn array of (last_index - first_index + 1) size to hold the query results.

Returns

MLResultMLResult_OkThe query executed successfully.
MLResultMLResult_InvalidParamOne of the parameters was incorrect.
MLResultMLResult_UnspecifiedFailureUnspecified failure occurred.
MLResultMLSpatialAnchorResult_NotLocalizedNot localized to a space.

Required Permissions:

  • None

Putting index bounds on the results allows the caller to only receive a subset of the total number of results generated by the query. This is useful as a form of pagination in the case of a large number of anchors in the current space. Indexing is zero-based so if there are N results in the query, then it is required that 0 <= first_index <= last_index < N.

API Level:

  • 20

MLSpatialAnchorGetResultString

const char * MLSpatialAnchorGetResultString(
MLResult result
)

Returns an ASCII string for MLSpatialAnchorResult and MLResult codes.

Parameters

MLResultresultThe input MLResult enum from MLSpatialAnchor functions.

Required Permissions:

  • None

Return: ASCII string containing readable version of result code.

API Level:

  • 31

MLSpatialAnchorGetLocalizationInfo

MLResult MLSpatialAnchorGetLocalizationInfo(
MLHandle handle,
MLSpatialAnchorLocalizationInfo * out_localization_info
)

Get the current localization status of the device.

Parameters

MLHandlehandleHandle to a Spatial Anchor tracker.
MLSpatialAnchorLocalizationInfo *out_localization_infoThe localization info structure to be populated.

Returns

MLResultMLResult_OkThe query executed successfully.
MLResultMLResult_InvalidParamOne of the parameters was incorrect.
MLResultMLResult_UnspecifiedFailureUnspecified failure occurred.

Required Permissions:

  • None
Deprecated

Deprecated since 1.3.0. Scheduled for removal. Replaced by MLSpaceGetLocalizationResult.

API Level:

  • 20