SpatialAnchorTransferManager.TryExportAnchorsAsync Method

Definition

Exports spatial anchors to a stream, which can later be imported on another device. This allows both devices to reason about the same locations in their users' surroundings.

public:
 static IAsyncOperation<bool> ^ TryExportAnchorsAsync(IIterable<IKeyValuePair<Platform::String ^, SpatialAnchor ^> ^> ^ anchors, IOutputStream ^ stream);
/// [Windows.Foundation.Metadata.RemoteAsync]
 static IAsyncOperation<bool> TryExportAnchorsAsync(IIterable<IKeyValuePair<winrt::hstring, SpatialAnchor const&>> const& anchors, IOutputStream const& stream);
/// [Windows.Foundation.Metadata.RemoteAsync]
/// [Windows.Foundation.Metadata.Deprecated("Use SpatialEntityStore instead of SpatialAnchorTransferManager. For more info, see MSDN.", Windows.Foundation.Metadata.DeprecationType.Deprecate, 262144, "Windows.Foundation.UniversalApiContract")]
 static IAsyncOperation<bool> TryExportAnchorsAsync(IIterable<IKeyValuePair<winrt::hstring, SpatialAnchor const&>> const& anchors, IOutputStream const& stream);
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<bool> TryExportAnchorsAsync(IEnumerable<KeyValuePair<string,SpatialAnchor>> anchors, IOutputStream stream);
[Windows.Foundation.Metadata.RemoteAsync]
[Windows.Foundation.Metadata.Deprecated("Use SpatialEntityStore instead of SpatialAnchorTransferManager. For more info, see MSDN.", Windows.Foundation.Metadata.DeprecationType.Deprecate, 262144, "Windows.Foundation.UniversalApiContract")]
public static IAsyncOperation<bool> TryExportAnchorsAsync(IEnumerable<KeyValuePair<string,SpatialAnchor>> anchors, IOutputStream stream);
function tryExportAnchorsAsync(anchors, stream)
Public Shared Function TryExportAnchorsAsync (anchors As IEnumerable(Of KeyValuePair(Of String, SpatialAnchor)), stream As IOutputStream) As IAsyncOperation(Of Boolean)

Parameters

anchors

IIterable<IKeyValuePair<String,SpatialAnchor>>

IEnumerable<KeyValuePair<String,SpatialAnchor>>

IIterable<IKeyValuePair<Platform::String,SpatialAnchor>>

IIterable<IKeyValuePair<winrt::hstring,SpatialAnchor>>

The collection of anchors to export, each identified by an app-specified string key.

stream
IOutputStream

The stream to export anchors to.

Returns

Operation that triggers once the export is complete.

Attributes

Windows requirements

App capabilities
spatialPerception

Remarks

It's the app's responsibility to get the data in the stream to the other device through its own network channel.

This method yields a result of true if the export succeeded. The export can fail if the spatial understanding system times out during the export.

Note: If you're using JavaScript, you can't create the anchors parameter directly, because it's of type IIterable<IKeyValuePair<Platform::String^, Windows::Perception::Spatial::SpatialAnchor^>>. Instead, create a native WinRT helper component that has a CreateMap function:

#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Perception.Spatial.h>

using namespace winrt;
using namespace Windows::Foundation::Collections;
using namespace Windows::Perception::Spatial;

IMap<winrt::hstring, SpatialAnchor> CreateMap()
{
    return winrt::single_threaded_map<winrt::hstring, SpatialAnchor>();
}
#include "pch.h"
#include "SpatialAnchorHelper.h"

using namespace SpatialHelper;
using namespace Platform;

Windows::Foundation::Collections::IMap<Platform::String^, Windows::Perception::Spatial::SpatialAnchor^>^ SpatialAnchorHelper::CreateMap()
{
    return ref new Platform::Collections::Map<Platform::String^, Windows::Perception::Spatial::SpatialAnchor^>();
}

Now you can populate the anchors collection in JavaScript and pass it to the TryExportAnchorsAsync method. The following code example shows how to use the SpatialAnchorHelper class to populate the anchors collection.

waitForPositionalTracking(function () {
	var spatialAnchor = Windows.Perception.Spatial.SpatialAnchor.tryCreateRelativeTo(stationaryRef.coordinateSystem);

	if (isLocatableRelativeToUser(spatialAnchor)) {
		var map = SpatialHelper.SpatialAnchorHelper.createMap();
		map.insert("test", spatialAnchor);

		var stream = Windows.Storage.Streams.InMemoryRandomAccessStream();

		console.log("Exporting spatial anchor");
		var exportWatch = new Stopwatch();
		Windows.Perception.Spatial.SpatialAnchorTransferManager.tryExportAnchorsAsync(map.getView(), stream.getOutputStreamAt(0)).then(
            function (succeeded) {
                if (succeeded) {
                    console.log("Exported " + stream.size + " bytes to stream.  Elapsed time: " + exportWatch.stop() + " seconds");
...

Applies to