Terrain anchors

A Terrain anchor is a type of Geospatial anchor that allows you to place AR objects using only latitude and longitude, leveraging information from Google Maps to find the precise altitude above ground.

Prerequisites

Make sure that you enable the Geospatial API before proceeding.

Set the plane-finding mode

The specified ARPlaneManager.PlaneDetectionMode plane detection mode controls the stability of placed Terrain anchors.

Mode Behavior Use case
Horizontal | Vertical (Everything) ARPlaneManager detects horizontal and vertical surfaces, such as floors and walls, as planes and accurately renders AR objects placed on them. Your app needs to recognize both horizontal and vertical surfaces.
Horizontal ARPlaneManager detects horizontal surfaces only. Your app needs to recognize horizontal surfaces only. Use to save computational power.
Vertical ARPlaneManager detects vertical surfaces only. Your app needs to recognize vertical surfaces only. Use to save computational power.
None (Nothing) Plane detection is disabled. May cause the vertical positioning of the Terrain anchor to be less accurate. Your app does not need accurate plane detection.

Use the Detection Mode drop-down menu to set the detection mode:

Create a Terrain anchor

To create and place a Terrain anchor, call ARGeospatialAnchor.

if (EarthManager.EarthState == EarthState.Enabled &&
          EarthManager.EarthTrackingState == TrackingState.Tracking)
{
    TerrainAnchor = AnchorManager.ResolveAnchorOnTerrain(
        /* Locational values */
        latitude,
        longitude,
        altitudeAboveTerrain,
        quaternion
    );
    // This anchor can't be used immediately; check its TerrainAnchorState before rendering
    // content on this anchor.
}

Creating anchors within 0.1 degrees of the North Pole or South Pole (90 degrees or -90 degrees) is not supported. AnchorManager.AddAnchor will return null if such anchors are attempted.

Check the Terrain anchor state

Once created, a Terrain anchor will have a state attached to it.

State Description
TaskInProgress The Terrain anchor has just been created, and ARCore is communicating with the ARCore API on Google Cloud to determine the anchor's exact pose.
Success The Terrain anchor has been successfully hosted and is now trackable. You can now use it to attach content to your AR experience.
Error state An error has occurred during the resolving process.

Check ARGeospatialAnchor.terrainAnchorState to check the current state of the Terrain anchor:

switch (TerrainAnchor.terrainAnchorState) {
    case TerrainAnchorState.Success:
        if (terrainAnchor.getTrackingState() == TrackingState.TRACKING) {
            renderAnchoredContent(terrainAnchor);
        }
        break;
    case TerrainAnchorState.TaskInProgress:
        // ARCore is contacting the ARCore API to resolve the Terrain anchor's pose.
        // Display some waiting UI.
        break;
    case TerrainAnchorState.ErrorUnsupportedLocation:
        // The requested anchor is in a location that isn't supported by the Geospatial API.
        break;
    case TerrainAnchorState.ErrorNotAuthorized:
        // An error occurred while authorizing your app with the ARCore API. See
        // https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#terrainanchorstate_errornotauthorized
        // for troubleshooting steps.
        break;
    case TerrainAnchorState.ErrorInternal:
        // The Terrain anchor could not be resolved due to an internal error.
        break;
    case TerrainAnchorState.None:
        // This Anchor isn't a Terrain anchor or it became invalid because the Geospatial Mode was
        // disabled.
        break;
}

What's next