Geospatial anchors are a type of [anchor](https://developers.google.com/ar/develop/anchors) 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](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#wgs84_anchors):   

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

2. [Terrain anchors](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#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](https://developers.google.com/ar/develop/geospatial#global_localization_with_vps).

3. [Rooftop anchors](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#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](https://developers.google.com/ar/develop/unity-arf/geospatial/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](https://developers.google.com/ar/develop/unity-arf/geospatial/enable) before proceeding.

## Place Geospatial anchors

Each anchor type has dedicated APIs to create them; see [Types of Geospatial anchors](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#types) for more information.

### Create an anchor from a hit-test

You can also create a Geospatial anchor from a [hit-test result](https://developers.google.com/ar/develop/hit-test).
Use the Pose from the hit-test and convert it to a [`GeospatialPose`](https://developers.google.com/ar/reference/unity-arf/struct/Google/XR/ARCoreExtensions/GeospatialPose). Use it to place any of the 3 anchor types described.

#### Get a Geospatial Pose from an AR Pose

[`AREarthManager.Convert(Pose)`](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/AREarthManager#convert) 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

[`AREarthManager.Convert(GeospatialPose)`](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/AREarthManager#convert_1) 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](https://developers.google.com/ar/develop/unity-arf/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](https://developers.google.com/ar/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](https://www.google.com/maps) on your desktop computer.

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-earth-turn-off-3d-buildings-2.png)
2. Navigate to **Layers** \> **More**.

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-maps-layers.png)

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-maps-more.png)
3. Change the **Map Type** to **Satellite** and clear the **Globe View** checkbox at the bottom left corner of the screen.

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-maps-map-type-satellite-globe-view.png)

   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.

> [!NOTE]
> **Note:** Obtaining these coordinates using Google Earth, as opposed to using Google Maps, will give you an error margin of up to several meters. We recommend using Google Maps and the Elevation API to get the latitude and longitude of a location.

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

1. Go to [Google Earth](https://earth.google.com/) on your desktop computer.

2. Navigate to the hamburger menu ![](https://developers.google.com/static/ar/develop/geospatial/images/hamburger-menu.png) and select **Map Style**.

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-earth-turn-off-3d-buildings-2.png)
3. Toggle off the **3D Buildings** switch.

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-earth-turn-off-3d-buildings-3.png)
4. Once the **3D Buildings** switch has been toggled off, click the pin icon ![](https://developers.google.com/static/ar/develop/geospatial/images/pin-icon.png) 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 ![](https://developers.google.com/static/ar/develop/geospatial/images/back-button.png) in the project pane and select the ![](https://developers.google.com/static/ar/develop/geospatial/images/more-actions-menu.png) **More Actions** menu.

8. Choose **Export as KML file** from the menu.

   <br />

   ![](https://developers.google.com/static/ar/develop/geospatial/images/google-earth-place-point-6.png)

   <br />

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

[`GeospatialPose.EunRotation`](https://developers.google.com/ar/reference/unity-arf/struct/Google/XR/ARCoreExtensions/GeospatialPose#eunrotation) 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-north (EUN) coordinate system. X+ points east, Y+ points up away from gravity, and Z+ points north.

## WGS84 Anchors

A WGS84 Anchor is a type of [anchor](https://developers.google.com/ar/develop/anchors) 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](https://en.wikipedia.org/wiki/World_Geodetic_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](https://developers.google.com/maps/documentation/elevation/overview) to get an elevation based on the [EGM96](https://en.wikipedia.org/wiki/Earth_Gravitational_Model) specification. You must convert the Maps API EGM96 elevation to WGS84 for comparison against the [`GeospatialPose`](https://developers.google.com/ar/reference/unity-arf/struct/Google/XR/ARCoreExtensions/GeospatialPose) altitude. See the [GeoidEval](https://geographiclib.sourceforge.io/cgi-bin/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](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#use-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 [`GeospatialPose`](https://developers.google.com/ar/reference/unity-arf/struct/Google/XR/ARCoreExtensions/GeospatialPose) without using another source, such as the Maps API.

#### Create the anchor

Once you have the latitude, longitude, altitude, and rotation quaternion, use [`ARAnchorManagerExtensions.AddAnchor()`](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARAnchorManagerExtensions#addanchor)
to anchor content to geographical coordinates that you specify.

    if (earthTrackingState == TrackingState.Tracking)
    {
      var anchor =
          AnchorManager.AddAnchor(
              latitude,
              longitude,
              altitude,
              quaternion);
      var anchoredAsset = Instantiate(GeospatialAssetPrefab, anchor.transform);
    }

> [!NOTE]
> **Note:** Creating anchors within 0.1 degrees of the North Pole or South Pole (90 degrees or -90 degrees) is not supported. If such an anchor is specified, the anchor will fail to be created.

## Terrain Anchors

A Terrain anchor is a type of [anchor](https://developers.google.com/ar/develop/anchors) that allows you to place AR objects using only latitude and longitude, leveraging information from [VPS](https://developers.google.com/ar/develop/geospatial#global_localization_with_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.

Note that terrain anchors are affected by [`Horizontal`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@4.2/api/UnityEngine.XR.ARSubsystems.PlaneDetectionMode.html) and [`Horizontal | Vertical`](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@4.2/api/UnityEngine.XR.ARSubsystems.PlaneDetectionMode.html)

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

![](https://developers.google.com/static/ar/develop/geospatial/images/ar-plane-manager-detection-mode.png)

### Create a Terrain anchor using the new Async API

To create and place a Terrain anchor, call [`ARAnchorManagerExtensions.resolveAnchorOnTerrainAsync()`](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARAnchorManagerExtensions#resolveanchoronterrainasync).

The anchor will not be ready right away and needs to resolve. Once it has resolved it will be available in the [`ResolveAnchorOnTerrainPromise`](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ResolveAnchorOnTerrainPromise).

    public GameObject TerrainAnchorPrefab;

    public void Update()
    {
        ResolveAnchorOnTerrainPromise terrainPromise =
            AnchorManager.ResolveAnchorOnTerrainAsync(
                latitude, longitude, altitudeAboveTerrain, eunRotation);

        // The anchor will need to be resolved.
        StartCoroutine(CheckTerrainPromise(terrainPromise));
    }

    private IEnumerator CheckTerrainPromise(ResolveAnchorOnTerrainPromise promise)
    {
        yield return promise;

        var result = promise.Result;
        if (result.TerrainAnchorState == TerrainAnchorState.Success &&
            result.Anchor != null)
        {
            // resolving anchor succeeded
            GameObject anchorGO = Instantiate(TerrainAnchorPrefab,
                result.Anchor.gameObject.transform);
            anchorGO.transform.parent = result.Anchor.gameObject.transform;
        }
        else
        {
           // resolving anchor failed
        }

        yield break;
    }

> [!NOTE]
> **Note:** Creating anchors within 0.1 degrees of the North Pole or South Pole (90 degrees or -90 degrees) is not supported. The [ResolveAnchorOnTerrainResult](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ResolveAnchorOnTerrainResult) will have a `TerrainAnchorState.None` and the anchor will be `null` if such anchors are attempted.

### Check the State of the Promise

The Promise will have an associated [`PromiseState`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate).

| State | Description |
|---|---|
| [`Pending`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) | The operation is still pending. |
| [`Done`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) | The operation is complete and the result is available. |
| [`Cancelled`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) | The operation has been cancelled. |

### Check the Terrain anchor state of the Promise result

The [`TerrainAnchorState`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#terrainanchorstate) belongs to the asynchronous operation and is part of the final Promise result.

    switch (result.TerrainAnchorState)
    {
        case TerrainAnchorState.Success:
            // Anchor has successfully resolved
            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;
        default:
            break;
    }

## Rooftop Anchors

![Rooftop anchors Hero](https://developers.google.com/static/ar/develop/geospatial/images/hero-rooftop-anchors.jpg)

Rooftop anchors are a type of [anchor](https://developers.google.com/ar/develop/anchors) and are very similar to [Terrain anchors](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#terrain_anchors) above. The difference is that you will provide the altitude above the rooftop rather than the altitude above the terrain.

> [!NOTE]
> **Note:** Some locations have more accurate rooftop information than others. Check out the Quality section in [Streetscape Geometry](https://developers.google.com/ar/develop/unity-arf/geospatial/streetscape-geometry) for more information.

### 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 [`ARAnchorManagerExtensions.resolveAnchorOnRooftopAsync()`](https://developers.google.com/ar/reference/unity-arf/class/Google/XR/ARCoreExtensions/ARAnchorManagerExtensions#resolveanchoronrooftopasync). Similarly to Terrain anchors, you will also access the [`PromiseState`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate) of the Promise. Then you can check the Promise result to access the [`RooftopAnchorState`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#rooftopanchorstate).

    public GameObject RooftopAnchorPrefab;

    public void Update()
    {
        ResolveAnchorOnRooftopPromise rooftopPromise =
            AnchorManager.ResolveAnchorOnRooftopAsync(
                latitude, longitude, altitudeAboveRooftop, eunRotation);

        // The anchor will need to be resolved.
        StartCoroutine(CheckRooftopPromise(rooftopPromise));
    }

    private IEnumerator CheckRooftopPromise(ResolveAnchorOnTerrainPromise promise)
    {
        yield return promise;

        var result = promise.Result;
        if (result.RooftopAnchorState == RooftopAnchorState.Success &&
            result.Anchor != null)
        {
            // resolving anchor succeeded
            GameObject anchorGO = Instantiate(RooftopAnchorPrefab,
                result.Anchor.gameObject.transform);
            anchorGO.transform.parent = result.Anchor.gameObject.transform;
        }
        else
        {
           // resolving anchor failed
        }

        yield break;
    }

### Check the State of the Promise

The Promise will have an associated [`PromiseState`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#promisestate), see the [table](https://developers.google.com/ar/develop/unity-arf/geospatial/anchors#check_the_state_of_the) above.

### Check the Rooftop anchor state of the Promise result

The [`RooftopAnchorState`](https://developers.google.com/ar/reference/unity-arf/namespace/Google/XR/ARCoreExtensions#rooftopanchorstate) belongs to the asynchronous operation and is part of the final Promise result.

    switch (result.RooftopAnchorState)
    {
        case TerrainAnchorState.Success:
            // Anchor has successfully resolved
            break;
        case RooftopAnchorState.ErrorUnsupportedLocation:
            // The requested anchor is in a location that isn't supported by the Geospatial API.
            break;
        case RooftopAnchorState.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 RooftopAnchorState.ErrorInternal:
            // The Rooftop anchor could not be resolved due to an internal error.
            break;
        default:
            break;
    }

## What's next

- Make sure you understand the Geospatial [API usage quota](https://developers.google.com/ar/develop/unity-arf/geospatial/api-usage-quota).