<br />

The Geospatial API uses a combination of [VPS](https://developers.google.com/ar/develop/geospatial#global_localization_with_vps) and GPS data to generate high-accuracy Geospatial poses. The API can be used in any place where the device is able to determine its location:

- In areas with low GPS accuracy, such as indoor spaces and dense urban environments, the API will rely on VPS coverage to generate high-accuracy poses.
- In outdoor environments with few or no overhead obstructions, the Geospatial API may be able to use available GPS location data to generate Geospatial poses with high accuracy.

You can determine VPS availability at a given horizontal position before the AR session starts and use it to create more specific experiences --- for example, to present an "Enter AR" button only when VPS is available.

## Enable the ARCore API

Your app must enable the [ARCore API](https://developers.google.com/ar/develop/c/geospatial/enable#enable_the_arcore_api) to check VPS availability.

Once the ARCore API is enabled, you can check VPS availability without:

- A current running [`ArSession`](https://developers.google.com/ar/reference/c/group/ar-session) (before calling [`ArSession_resume()`](https://developers.google.com/ar/reference/c/group/ar-session#arsession_resume)).
- Setting a [`ArGeospatialMode`](https://developers.google.com/ar/reference/c/group/ar-config#argeospatialmode).

## Check VPS availability in your app

The Geospatial API can be used in any place where the device is able to determine its location. If your AR experience hinges on VPS coverage, you can use [`ArSession_checkVpsAvailabilityAsync()`](https://developers.google.com/ar/reference/c/group/ar-session#arsession_checkvpsavailabilityasync) to obtain a [`ArVpsAvailabilityFuture`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arvpsavailabilityfuture), an asynchronous task that checks the VPS availability at a given horizontal position.

Once you have the [`ArVpsAvailabilityFuture`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arvpsavailabilityfuture), you can obtain its result by polling or through a callback.


### Poll the result

Use [`ArFuture_getState()`](https://developers.google.com/ar/reference/c/group/ar-future#arfuture_getstate) to obtain the state of the `ArFuture`. There are three different states:

- [`AR_FUTURE_STATE_PENDING`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arfuturestate): The operation is not yet complete, so no result is known.
- [`AR_FUTURE_STATE_CANCELLED`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arfuturestate): The operation has been cancelled by [`ArFuture_cancel()`](https://developers.google.com/ar/reference/c/group/ar-future#arfuture_cancel). Any registered callback will never be called.
- [`AR_FUTURE_STATE_DONE`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arfuturestate): The operation is complete. Use [`ArVpsAvailabilityFuture_getResult()`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arvpsavailabilityfuture_getresult) to obtain the result.

You may continue checking [`ArFuture_getState()`](https://developers.google.com/ar/reference/c/group/ar-future#arfuture_getstate) until the task is complete.

```c
// Obtain a ArVpsAvailabilityFuture and store it somewhere.
ArVpsAvailabilityFuture* future = NULL;
CHECK(ArSession_checkVpsAvailabilityAsync(ar_session, latitude, longitude,
                                          NULL, NULL, &future) == AR_SUCCESS);

// Poll ArVpsAvailabilityFuture later, for example, in a render loop.
ArFutureState future_state = AR_FUTURE_STATE_PENDING;
ArFuture_getState(ar_session, (ArFuture*)future, &future_state);
if (future_state == AR_FUTURE_STATE_DONE) {
  ArVpsAvailability vps_availability = AR_VPS_AVAILABILITY_UNKNOWN;
  ArVpsAvailabilityFuture_getResult(ar_session, future, &vps_availability);
  switch (vps_availability) {
    case AR_VPS_AVAILABILITY_AVAILABLE:
      // VPS is available at this location.
      break;
    case AR_VPS_AVAILABILITY_UNAVAILABLE:
      // VPS is unavailable at this location.
      break;
    case AR_VPS_AVAILABILITY_ERROR_NETWORK_CONNECTION:
      // The external service could not be reached due to a network connection
      // error.
      break;

      // Handle other error states, e.g.
      // AR_VPS_AVAILABILITY_ERROR_RESOURCE_EXHAUSTED,
      // AR_VPS_AVAILABILITY_ERROR_INTERNAL, ...
  }
  ArFuture_release((ArFuture*)future);
}
```

### Obtain the result through a callback

You can also obtain the result of a `ArFuture` through a callback. Use [`ArSession_checkVpsAvailabilityAsync()`](https://developers.google.com/ar/reference/c/group/ar-session#arsession_checkvpsavailabilityasync) and supply a `callback`. This `callback` will be called on the Main thread soon after the `ArFuture` has state [`AR_FUTURE_STATE_DONE`](https://developers.google.com/ar/reference/c/group/ar-vps-availability-future#arfuturestate).

```c
void vps_availability_callback(void* context, ArVpsAvailability availability) {
  // Callback is called on the Main thread.

  // Handle the ArVpsAvailability result as shown above.
  // For example, show UI that enables your AR view.

  // It is a best practice to free `context` memory at the end of the callback.
  free(context);
}

void check_availability_with_callback(ArSession* ar_session, double latitude,
                                      double longitude) {
  ArVpsAvailabilityFuture* future = NULL;
  void* context = NULL;
  CHECK(ArSession_checkVpsAvailabilityAsync(
      ar_session, latitude, longitude, context, vps_availability_callback,
      &future) == AR_SUCCESS);
}
```

> [!NOTE]
> **Note:** When calling [`ArSession_checkVpsAvailabilityAsync()`](https://developers.google.com/ar/reference/c/group/ar-session#arsession_checkvpsavailabilityasync), you may provide an optional `context` parameter. This `context` is passed to the callback, which can be useful to associate additional data to a request. It is best practice to free the memory of `context` at the end of the callback and when [`ArFuture_cancel()`](https://developers.google.com/ar/reference/c/group/ar-future#arfuture_cancel) successfully cancels the future.

## Cancel the `ArFuture`

Use [`ArFuture_cancel()`](https://developers.google.com/ar/reference/c/group/ar-future#arfuture_cancel) to attempt to cancel the `ArFuture`. Due to thread parallelism, it may be possible that your cancel attempt does not actually succeed.

`ArFuture_cancel()` returns `1` if this attempt was successful, and `0` otherwise.

## Use the Geospatial API without VPS coverage

The Geospatial API can also be used in areas that do not have VPS coverage. In outdoor environments with few or no overhead obstructions, GPS may be sufficient to generate a pose with high accuracy.

## What's next

- [Obtain the device camera's Geospatial pose](https://developers.google.com/ar/develop/c/geospatial/obtain-device-pose) to determine the exact location of the user's device in the real world.