检查设备当前所在位置的 VPS 是否可用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Geospatial API 会结合使用 VPS 和 GPS 数据来生成高精度地理空间姿势。此 API 可在设备能够确定其位置的任何位置使用:
- 在 GPS 精度较低的区域(例如室内空间和密集的城市环境)中,该 API 将依赖于 VPS 覆盖范围来生成高精度姿势。
- 在没有或几乎没有头顶遮挡物的室外环境中,Geospatial API 或许能够使用可用的 GPS 位置数据生成高精度的地理空间姿势。
您可以在 AR 会话开始之前确定给定水平位置的 VPS 可用性,并据此打造更具体、更个性化的体验,例如,仅在 VPS 可用时显示“进入 AR”按钮。
启用 ARCore API
您的应用必须启用 ARCore API 才能检查 VPS 是否可用。
启用 ARCore API 后,您无需执行以下操作即可检查 VPS 的可用性:
检查应用中的 VPS 可用性
Geospatial API 可在设备能够确定其位置的任何位置使用。如果您的 AR 体验取决于 VPS 覆盖率,您可以使用 ArSession_checkVpsAvailabilityAsync()
获取 ArVpsAvailabilityFuture
,这是一个用于检查给定水平位置的 VPS 可用性的异步任务。
获取 ArVpsAvailabilityFuture
后,您可以通过轮询或回调来获取其结果。
轮询结果
使用 ArFuture_getState()
获取 ArFuture
的状态。有三种不同的状态:
您可以继续查看 ArFuture_getState()
,直到任务完成。
// 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);
}
通过回调获取结果
您还可以通过回调获取 ArFuture
的结果。使用 ArSession_checkVpsAvailabilityAsync()
并提供 callback
。在 ArFuture
的状态变为 AR_FUTURE_STATE_DONE
后,系统会在主线程上立即调用此 callback
。
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);
}
取消 ArFuture
使用 ArFuture_cancel()
尝试取消 ArFuture
。由于线程并行性,您的取消尝试可能实际上并未成功。
如果此尝试成功,ArFuture_cancel()
会返回 1
;否则,会返回 0
。
在没有 VPS 覆盖的情况下使用 Geospatial API
Geospatial API 还可在未覆盖 VPS 的区域使用。在头顶上方没有或几乎没有遮挡物的室外环境中,GPS 可能就足以生成高精度的姿势。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe Geospatial API leverages VPS and GPS data to create precise location anchors, enhancing AR experiences in various environments.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can proactively confirm VPS availability at a specific location using the \u003ccode\u003eArSession_checkVpsAvailabilityAsync()\u003c/code\u003e function before initiating an AR session.\u003c/p\u003e\n"],["\u003cp\u003eThe API offers flexible methods for retrieving VPS availability results, including polling using \u003ccode\u003eArFuture_getState()\u003c/code\u003e or utilizing a callback function for asynchronous updates.\u003c/p\u003e\n"],["\u003cp\u003eEven in areas without VPS coverage, the Geospatial API can still provide location data by relying on GPS, particularly in outdoor settings with clear sky visibility.\u003c/p\u003e\n"],["\u003cp\u003eAfter confirming VPS availability or utilizing GPS, developers can obtain the device's Geospatial pose to accurately position virtual content in the real world.\u003c/p\u003e\n"]]],["The Geospatial API leverages VPS and GPS data for accurate positioning. To check VPS availability, enable the ARCore API and use `ArSession_checkVpsAvailabilityAsync()` to create an `ArVpsAvailabilityFuture`. Results can be obtained by polling the `ArFuture` state with `ArFuture_getState()` or via a callback, indicating whether VPS is available, unavailable, or encountering network errors. The `ArFuture` can be canceled using `ArFuture_cancel()`. Even without VPS, the API utilizes GPS for positioning.\n"],null,["# Check VPS availability at the device's current location\n\n\u003cbr /\u003e\n\nThe Geospatial API uses a combination of [VPS](/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:\n\n- 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.\n- 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.\n\nYou 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.\n\nEnable the ARCore API\n---------------------\n\nYour app must enable the [ARCore API](/ar/develop/c/geospatial/enable#enable_the_arcore_api) to check VPS availability.\n\nOnce the ARCore API is enabled, you can check VPS availability without:\n\n- A current running [`ArSession`](/ar/reference/c/group/ar-session) (before calling [`ArSession_resume()`](/ar/reference/c/group/ar-session#arsession_resume)).\n- Setting a [`ArGeospatialMode`](/ar/reference/c/group/ar-config#argeospatialmode).\n\nCheck VPS availability in your app\n----------------------------------\n\nThe 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()`](/ar/reference/c/group/ar-session#arsession_checkvpsavailabilityasync) to obtain a [`ArVpsAvailabilityFuture`](/ar/reference/c/group/ar-vps-availability-future#arvpsavailabilityfuture), an asynchronous task that checks the VPS availability at a given horizontal position.\n\nOnce you have the [`ArVpsAvailabilityFuture`](/ar/reference/c/group/ar-vps-availability-future#arvpsavailabilityfuture), you can obtain its result by polling or through a callback.\n\n\n### Poll the result\n\nUse [`ArFuture_getState()`](/ar/reference/c/group/ar-future#arfuture_getstate) to obtain the state of the `ArFuture`. There are three different states:\n\n- [`AR_FUTURE_STATE_PENDING`](/ar/reference/c/group/ar-vps-availability-future#arfuturestate): The operation is not yet complete, so no result is known.\n- [`AR_FUTURE_STATE_CANCELLED`](/ar/reference/c/group/ar-vps-availability-future#arfuturestate): The operation has been cancelled by [`ArFuture_cancel()`](/ar/reference/c/group/ar-future#arfuture_cancel). Any registered callback will never be called.\n- [`AR_FUTURE_STATE_DONE`](/ar/reference/c/group/ar-vps-availability-future#arfuturestate): The operation is complete. Use [`ArVpsAvailabilityFuture_getResult()`](/ar/reference/c/group/ar-vps-availability-future#arvpsavailabilityfuture_getresult) to obtain the result.\n\nYou may continue checking [`ArFuture_getState()`](/ar/reference/c/group/ar-future#arfuture_getstate) until the task is complete. \n\n```c\n// Obtain a ArVpsAvailabilityFuture and store it somewhere.\nArVpsAvailabilityFuture* future = NULL;\nCHECK(ArSession_checkVpsAvailabilityAsync(ar_session, latitude, longitude,\n NULL, NULL, &future) == AR_SUCCESS);\n\n// Poll ArVpsAvailabilityFuture later, for example, in a render loop.\nArFutureState future_state = AR_FUTURE_STATE_PENDING;\nArFuture_getState(ar_session, (ArFuture*)future, &future_state);\nif (future_state == AR_FUTURE_STATE_DONE) {\n ArVpsAvailability vps_availability = AR_VPS_AVAILABILITY_UNKNOWN;\n ArVpsAvailabilityFuture_getResult(ar_session, future, &vps_availability);\n switch (vps_availability) {\n case AR_VPS_AVAILABILITY_AVAILABLE:\n // VPS is available at this location.\n break;\n case AR_VPS_AVAILABILITY_UNAVAILABLE:\n // VPS is unavailable at this location.\n break;\n case AR_VPS_AVAILABILITY_ERROR_NETWORK_CONNECTION:\n // The external service could not be reached due to a network connection\n // error.\n break;\n\n // Handle other error states, e.g.\n // AR_VPS_AVAILABILITY_ERROR_RESOURCE_EXHAUSTED,\n // AR_VPS_AVAILABILITY_ERROR_INTERNAL, ...\n }\n ArFuture_release((ArFuture*)future);\n}\n```\n\n### Obtain the result through a callback\n\nYou can also obtain the result of a `ArFuture` through a callback. Use [`ArSession_checkVpsAvailabilityAsync()`](/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`](/ar/reference/c/group/ar-vps-availability-future#arfuturestate). \n\n```c\nvoid vps_availability_callback(void* context, ArVpsAvailability availability) {\n // Callback is called on the Main thread.\n\n // Handle the ArVpsAvailability result as shown above.\n // For example, show UI that enables your AR view.\n\n // It is a best practice to free `context` memory at the end of the callback.\n free(context);\n}\n\nvoid check_availability_with_callback(ArSession* ar_session, double latitude,\n double longitude) {\n ArVpsAvailabilityFuture* future = NULL;\n void* context = NULL;\n CHECK(ArSession_checkVpsAvailabilityAsync(\n ar_session, latitude, longitude, context, vps_availability_callback,\n &future) == AR_SUCCESS);\n}\n```\n| **Note:** When calling [`ArSession_checkVpsAvailabilityAsync()`](/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()`](/ar/reference/c/group/ar-future#arfuture_cancel) successfully cancels the future.\n\nCancel the `ArFuture`\n---------------------\n\nUse [`ArFuture_cancel()`](/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.\n\n`ArFuture_cancel()` returns `1` if this attempt was successful, and `0` otherwise.\n\nUse the Geospatial API without VPS coverage\n-------------------------------------------\n\nThe 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.\n\nWhat's next\n-----------\n\n- [Obtain the device camera's Geospatial pose](/ar/develop/c/geospatial/obtain-device-pose) to determine the exact location of the user's device in the real world."]]