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 chosen plane detection mode controls the stability of placed Terrain anchors. Use ArPlaneFindingMode
to select how your app detects planes.
Mode | Behavior | Use case |
---|---|---|
AR_PLANE_FINDING_MODE_HORIZONTAL_AND_VERTICAL
|
Default mode. ARCore 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. |
AR_PLANE_FINDING_MODE_HORIZONTAL
|
ARCore detects horizontal surfaces only. | Your app needs to recognize horizontal surfaces only. Use to save computational power. |
AR_PLANE_FINDING_MODE_VERTICAL
|
ARCore detects vertical surfaces only. | Your app needs to recognize vertical surfaces only. Use to save computational power. |
AR_PLANE_FINDING_MODE_DISABLED
|
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. |
Create a Terrain anchor
To create and place a Terrain anchor, call ArEarth_resolveAndAcquireNewAnchorOnTerrain()
.
float eus_quaternion_4[4] = {qx, qy, qz, qw}; if (ar_earth != NULL) { ArTrackingState earth_tracking_state = AR_TRACKING_STATE_STOPPED; ArTrackable_getTrackingState(ar_session, (ArTrackable*)ar_earth, &earth_tracking_state); if (earth_tracking_state == AR_TRACKING_STATE_TRACKING) { CHECK(ArEarth_resolveAndAcquireNewAnchorOnTerrain( ar_session, ar_earth, /* Locational values */ latitude, longitude, altitude_above_terrain, eus_quaternion_4, &terrain_anchor)); // This anchor can't be used immediately; check its ArTrackingState // and ArTerrainAnchorState before rendering content on this anchor. } }
Creating anchors within 0.1 degrees of the North or South Poles (90
degrees or -90 degrees) is not supported. If such an anchor is specified, ArEarth_resolveAndAcquireNewAnchorOnTerrain()
will return an AR_ERROR_INVALID_ARGUMENT
and the anchor will fail to be created.
Check the Terrain anchor state
Once created, a Terrain anchor will have a state attached to it.
State | Description |
---|---|
AR_TERRAIN_ANCHOR_STATE_TASK_IN_PROGRESS |
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. |
AR_TERRAIN_ANCHOR_STATE_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 ArTerrainAnchorState
to check the current state of the Terrain anchor:
ArTerrainAnchorState terrain_anchor_state; ArAnchor_getTerrainAnchorState(ar_session, terrain_anchor, &terrain_anchor_state); ArTrackingState tracking_state; switch (terrain_anchor_state) { case AR_TERRAIN_ANCHOR_STATE_SUCCESS: ArAnchor_getTrackingState(ar_session, terrain_anchor, &tracking_state); if (tracking_state == AR_TRACKING_STATE_TRACKING) { render_anchored_content(terrain_anchor); } break; case AR_TERRAIN_ANCHOR_STATE_TASK_IN_PROGRESS: // ARCore is contacting the ARCore API to resolve the Terrain anchor's // pose. Display some waiting UI. break; case AR_TERRAIN_ANCHOR_STATE_ERROR_UNSUPPORTED_LOCATION: // The requested anchor is in a location that isn't supported by the // Geospatial API. break; case AR_TERRAIN_ANCHOR_STATE_ERROR_NOT_AUTHORIZED: // An error occurred while authorizing your app with the ARCore API. See // https://developers.devsite.corp.google.com/ar/reference/c/group/ar-anchor#arterrainanchorstate // for troubleshooting steps. break; case AR_TERRAIN_ANCHOR_STATE_ERROR_INTERNAL: // The Terrain anchor could not be resolved due to an internal error. break; case AR_TERRAIN_ANCHOR_STATE_NONE: // This Anchor isn't a Terrain anchor or it became invalid because the // Geospatial Mode was disabled. break; }
What's next
- Make sure you understand the Geospatial API usage quota.