แก้ไข

แชร์ผ่าน


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

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.

Prerequisites

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.

Important

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

// Create the sensor fingerprint provider
std::shared_ptr<PlatformLocationProvider> sensorProvider;
sensorProvider = std::make_shared<PlatformLocationProvider>();

// Allow GPS
const std::shared_ptr<SensorCapabilities>& sensors = sensorProvider->Sensors();
sensors->GeoLocationEnabled(true);

// Allow WiFi scanning
sensors->WifiEnabled(true);

// Populate the set of known BLE beacons' UUIDs
std::vector<std::string> uuids;
uuids.push_back("22e38f1a-c1b3-452b-b5ce-fdb0f39535c1");
uuids.push_back("a63819b9-8b7b-436d-88ec-ea5d8db2acb0");

// Allow the set of known BLE beacons
sensors->BluetoothEnabled(true);
sensors->KnownBeaconProximityUuids(uuids);

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
cloudSpatialAnchorSession->LocationProvider(sensorProvider);

// Configure the near-device criteria
auto nearDeviceCriteria = std::make_shared<NearDeviceCriteria>();
nearDeviceCriteria->DistanceInMeters(5.0f);
nearDeviceCriteria->MaxResultCount(25);

// Set the session's locate criteria
auto anchorLocateCriteria = std::make_shared<AnchorLocateCriteria>();
anchorLocateCriteria->NearDevice(nearDeviceCriteria);
cloudSpatialAnchorSession->CreateWatcher(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.