How to create and locate anchors using coarse relocalization in Swift
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:
- Basic knowledge of Swift.
- Read through the Azure Spatial Anchors overview.
- Completed one of the 5-minute Quickstarts.
- Read through the Create and locate anchors how-to.
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
var sensorProvider: ASAPlatformLocationProvider?
sensorProvider = ASAPlatformLocationProvider()
// Allow GPS
let sensors = locationProvider?.sensors
sensors.geoLocationEnabled = true
// Allow WiFi scanning
sensors.wifiEnabled = true
// Populate the set of known BLE beacons' UUIDs
let uuids = [String]()
uuids.append("22e38f1a-c1b3-452b-b5ce-fdb0f39535c1")
uuids.append("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
let nearDeviceCriteria = ASANearDeviceCriteria()!
nearDeviceCriteria.distanceInMeters = 5.0
nearDeviceCriteria.maxResultCount = 25
// Set the session's locate criteria
let anchorLocateCriteria = ASAAnchorLocateCriteria()!
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.