How to create and locate anchors using coarse relocalization in C++/WinRT

Azure Spatial Anchors can associate on-device, positioning sensor data with the anchors you create. This data can also be used to quickly determine whether there are any anchors nearby your device. For more information, see Coarse relocalization.


To complete this guide, make sure you have:

Configure the sensor fingerprint provider

We'll start by creating and configuring a sensor fingerprint provider. The sensor fingerprint provider will take care of reading the platform-specific sensors on your device and converting their readings into a common representation consumed by the cloud spatial anchor session.


Make sure to check here if the sensors you are enabling are available on your platform.

// Create the sensor fingerprint provider
PlatformLocationProvider sensorProvider = PlatformLocationProvider();

// Allow GPS
SensorCapabilities sensors = sensorProvider.Sensors()

// Allow WiFi scanning

// Populate the set of known BLE beacons' UUIDs
std::vector<winrt::hstring> uuids;

// Allow the set of known BLE beacons

Configure the cloud spatial anchor session

We'll take care of configuring the cloud spatial anchor session next. On the first line, we set the sensor provider on the session. From now on, any anchor we create during the session will be associated with a set of sensor readings. Next, we instantiate a near-device locate criteria and initialize it to match the application requirements. Finally, we instruct the session to use sensor data when locating anchors by creating a watcher from our near-device criteria.

// Set the session's sensor fingerprint provider

// Configure the near-device criteria
NearDeviceCriteria nearDeviceCriteria = NearDeviceCriteria();

// Set the session's locate criteria
anchorLocateCriteria = AnchorLocateCriteria();

After your watcher is created, the AnchorLocated event will fire for every anchor requested. This event fires when an anchor is located, or if the anchor can't be located. If this situation happens, the reason will be stated in the status. After all anchors for a watcher are processed, found or not found, then the LocateAnchorsCompleted event will fire. There is a limit of 35 identifiers per watcher.

Next steps

In this guide, you learned about how to create and locate anchors using on-device sensors. To learn more about coarse relocalization, continue to the next guide.