Use Geospatial anchors to position real-world content on Android NDK

Geospatial anchors are a type of anchor that allow you to place 3D content in the real world.

Types of Geospatial anchors

There are three types of Geospatial anchors, which each handle altitude differently:

  1. WGS84 anchors:
    WGS84 anchors let you place 3D content at any given latitude, longitude, and altitude.

  2. Terrain anchors:
    Terrain anchors let you place content using only latitude and longitude with a height relative to the terrain at that position. The altitude is determined relative to the ground or floor as known by VPS.

  3. Rooftop anchors:
    Rooftop anchors let you place content using only latitude and longitude with a height relative to a building's rooftop at that position. The altitude is determined relative to the top of a building as known by Streetscape Geometry. This will default to terrain altitude when not placed on a building.

WGS84 Terrain Rooftop
Horizontal Position Latitude, Longitude Latitude, Longitude Latitude, Longitude
Vertical Position Relative to WGS84 altitude Relative to terrain level determined by Google Maps Relative to rooftop level determined by Google Maps
Needs to be server-resolved? No Yes Yes

Prerequisites

Make sure that you enable the Geospatial API before proceeding.

Place Geospatial anchors

Each anchor type has dedicated APIs to create them; see Types of Geospatial anchors for more information.

Create an anchor from a hit-test

You can also create a Geospatial anchor from a hit-test result. Use the Pose from the hit-test and convert it to a ArGeospatialPose. Use it to place any of the 3 anchor types described.

Get a Geospatial Pose from an AR Pose

ArEarth_getGeospatialPose() provides an additional way to determine the latitude and longitude by converting an AR Pose to a Geospatial Pose.

Get an AR Pose from a Geospatial Pose

ArEarth_getPose() converts an Earth-specified horizontal position, altitude, and quaternion rotation with respect to an east-up-south coordinate frame to an AR Pose with respect to the GL world coordinate.

Choose which method suits your use case

Each method of creating an anchor has associated tradeoffs to keep in mind:

  • When using Streetscape Geometry, use a hit-test to attach content to a building.
  • Prefer Terrain or Rooftop anchors over WGS84 anchors because they use altitude values determined by Google Maps.

Determine the latitude and longitude of a location

There are three ways you can calculate a location’s latitude and longitude:

  • Use Geospatial Creator to view and augment the world with 3D content without having to physically go to a location. This allows you to place 3D immersive content visually using Google maps in the Unity Editor. The content’s latitude, longitude, rotation and altitude will be automatically calculated for you.
  • Use Google Maps
  • Use Google Earth. Note that obtaining these coordinates using Google Earth, as opposed to Google Maps, will give you an error margin of up to several meters.
  • Go to the physical location

Use Google Maps

To get the latitude and longitude of a location using Google Maps:

  1. Go to Google Maps on your desktop computer.

  2. Navigate to Layers > More.

  3. Change the Map Type to Satellite and clear the Globe View checkbox at the bottom left corner of the screen.

    This will force a 2D perspective and eliminate possible errors that could come from an angled 3D view.

  4. On the map, right-click on the location and select the longitude/latitude to copy it to your clipboard.

Use Google Earth

You can calculate a location’s latitude and longitude from Google Earth by clicking a location in the UI and reading the data from placemark details.

To get the latitude and longitude of a location using Google Earth:

  1. Go to Google Earth on your desktop computer.

  2. Navigate to the hamburger menu and select Map Style.

  3. Toggle off the 3D Buildings switch.

  4. Once the 3D Buildings switch has been toggled off, click the pin icon to add a placemark at the selected location.

  5. Specify a project to contain your placemark and click Save.

  6. In the Title field for the placemark, enter a name for the placemark.

  7. Click the back arrow in the project pane and select the More Actions menu.

  8. Choose Export as KML file from the menu.

The KLM file reports the latitude, longitude, and altitude for a placemark in the <coordinates> tag separated by commas, as follows:

<coordinates>-122.0755182435043,37.41347299422944,7.420342565583832</coordinates>

Do not use the latitude and longitude from the <LookAt> tags, which specify the camera position, not the location.

Go to the physical location

You can calculate a location’s altitude by going there physically and doing a local observation.

Get the rotation quaternion

ArGeospatialPose_getEastUpSouthQuaternion() extracts the orientation from a Geospatial Pose and outputs a quaternion that represents the rotation matrix transforming a vector from the target to the east-up-south (EUS) coordinate system. X+ points east, Y+ points up, and Z+ points south. Values are written in the order {x, y, z, w}.

WGS84 Anchors

A WGS84 Anchor is a type of anchor that allows you to place 3D content at any given latitude, longitude, and altitude. It relies on a Pose, and orientation, to be placed in the real world. The position consists of a latitude, longitude, and altitude, which are specified in the WGS84 coordinate system. The orientation consists of a quaternion rotation.

Altitude is reported in meters above the reference WGS84 ellipsoid such that ground level is not at zero. Your app is responsible for providing these coordinates for each created anchor.

Place a WGS84 anchor in the real world

Determine the altitude of a location

There are a few ways to determine a location’s altitude for placing anchors:

  • If the anchor's location is physically near the user, you can use an altitude that's similar to the altitude of the user’s device.
  • Once you have the latitude and longitude, use the Elevation API to get an elevation based on the EGM96 specification. You must convert the Maps API EGM96 elevation to WGS84 for comparison against the ArGeospatialPose altitude. See the GeoidEval that has both a command line and an HTML interface. The Maps API reports latitude and longitude according to the WGS84 specification out of the box.
  • You can obtain a location’s latitude, longitude, and altitude from Google Earth. This will give you an error margin of up to several meters. Use the latitude, longitude, and altitude from the <coordinates> tags, not the <LookAt> tags, in the KML file.
  • If an existing anchor is close by and if you are not on a steep incline, you may be able to use the altitude from the camera’s ArGeospatialPose without using another source, such as the Maps API.

Create the anchor

Once you have the latitude, longitude, altitude, and rotation quaternion, use ArEarth_acquireNewAnchor() to anchor content to geographical coordinates that you specify.

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) {
    ArAnchor* earth_anchor = NULL;
    ArStatus status = ArEarth_acquireNewAnchor(ar_session, ar_earth,
        /* location values */
        latitude, longitude, altitude,
        eus_quaternion_4, &earth_anchor);

    // Attach content to the anchor specified by geodetic location and
    // pose.
  }
}

Terrain Anchors

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

Rather than inputting the desired altitude, you provide the altitude above the terrain. When this is zero the anchor will be level with the terrain.

Set the plane-finding mode

Plane finding is optional and not required to utilize anchors. Note that only horizontal planes are used. Horizontal planes will help the dynamic alignment of terrain anchors on ground.

Use ArPlaneFindingMode to select how your app detects planes.

Create a Terrain anchor using the new Async API

To create and place a Terrain anchor, call ArEarth_resolveAnchorOnTerrainAsync().

The anchor will not be ready right away and needs to resolve. Once it has resolved it will be available in the ArResolveAnchorOnTerrainFuture.

Check the terrain anchor state using ArResolveAnchorOnTerrainFuture_getResultTerrainAnchorState(). Get the resolved anchor using ArResolveAnchorOnTerrainFuture_acquireResultAnchor().

float eus_quaternion_4[4] = {qx, qy, qz, qw};
void* context = NULL;
ArResolveAnchorOnTerrainCallback callback = NULL;
ArResolveAnchorOnTerrainFuture* future = NULL;
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) {
    ArStatus status = ArEarth_resolveAnchorOnTerrainAsync(
        ar_session, ar_earth,
        /* location values */
        latitude, longitude, altitude_above_terrain, eus_quaternion_4,
        context, callback, &future);
  }
}

Check the State of the Future

The Future will have an associated ArFutureState.

State Description
AR_FUTURE_STATE_PENDING The operation is still pending.
AR_FUTURE_STATE_DONE The operation is complete and the result is available.
AR_FUTURE_STATE_CANCELLED The operation has been cancelled.

Check the Terrain anchor state of the Future result

The ArTerrainAnchorState belongs to the asynchronous operation and is part of the final Future result.

switch (terrain_anchor_state) {
  case AR_TERRAIN_ANCHOR_STATE_SUCCESS:
    // A resolving task for this anchor has been successfully resolved.
    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.google.com/ar/reference/c/group/ar-anchor#:~:text=from%20this%20error.-,AR_TERRAIN_ANCHOR_STATE_ERROR_NOT_AUTHORIZED,-The%20authorization%20provided
    // for troubleshooting steps.
    break;
  case AR_TERRAIN_ANCHOR_STATE_ERROR_INTERNAL:
    // The Terrain anchor could not be resolved due to an internal error.
    break;
  default:
    break;
}

Rooftop Anchors

Rooftop anchors Hero

Rooftop anchors are a type of anchor and are very similar to Terrain anchors above. The difference is that you will provide the altitude above the rooftop rather than the altitude above the terrain.

Create a Rooftop anchor using the new Async API

The anchor will not be ready right away and needs to resolve.

To create and place a Rooftop anchor, call ArEarth_resolveAnchorOnRooftopAsync(). Similarly to Terrain anchors, you will also access the ArFutureState of the Future. Then you can check the Future result to access the ArRooftopAnchorState.

Use ArEarth_resolveAnchorOnRooftopAsync() to create a ArResolveAnchorOnRooftopFuture.

Check the rooftop anchor state using ArResolveAnchorOnRooftopFuture_getResultRooftopAnchorState().

Get the resolved anchor using ArResolveAnchorOnRooftopFuture_acquireResultAnchor().

float eus_quaternion_4[4] = {qx, qy, qz, qw};
void* context = NULL;
ArResolveAnchorOnRooftopCallback callback = NULL;
ArResolveAnchorOnRooftopFuture* future = NULL;
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) {
    ArStatus status = ArEarth_resolveAnchorOnRooftopAsync(
        ar_session, ar_earth,
        /* location values */
        latitude, longitude, altitude_above_rooftop, eus_quaternion_4,
        context, callback, &future);
  }
}

Check the State of the Future

The Future will have an associated ArFutureState, see the table above.

Check the Rooftop anchor state of the Future result

The ArRooftopAnchorState belongs to the asynchronous operation and is part of the final Future result.

switch (rooftop_anchor_state) {
  case AR_ROOFTOP_ANCHOR_STATE_SUCCESS:
    // A resolving task for this anchor has been successfully resolved.
    break;
  case AR_ROOFTOP_ANCHOR_STATE_ERROR_UNSUPPORTED_LOCATION:
    // The requested anchor is in a location that isn't supported by the
    // Geospatial API.
    break;
  case AR_ROOFTOP_ANCHOR_STATE_ERROR_NOT_AUTHORIZED:
    // An error occurred while authorizing your app with the ARCore API. See
    // https://developers.google.com/ar/reference/c/group/ar-anchor#:~:text=from%20this%20error.-,AR_ROOFTOP_ANCHOR_STATE_ERROR_NOT_AUTHORIZED,-The%20authorization%20provided
    // for troubleshooting steps.
    break;
  case AR_ROOFTOP_ANCHOR_STATE_ERROR_INTERNAL:
    // The Rooftop anchor could not be resolved due to an internal error.
    break;
  default:
    break;
}

What's next