通知:Google Maps Platform 即将推出全新基本地图样式。此次地图样式更新包括全新默认调色板、经过现代化改进的图钉,以及地图体验和易用性方面的改进。所有地图样式将于 2025 年 3 月自动更新。如需详细了解适用范围以及如何提前选择启用,请参阅
全新 Google Maps Platform 地图样式。
迁移概览
本指南重点介绍了旧版地点服务与新版地点类之间的主要区别。升级到酒店类会带来显著优势,包括提升了性能和采用了新的定价模式。为了充分利用地点,并确保您的应用是最新版本,请熟悉本指南中详述的变更。
迁移的结算最佳实践
warning_amber
如果您的 API 用量足够高,达到了第二层级的价格,则适用此指南。迁移到较新版本的 API 时,您还需要按其他 SKU 付费。为避免在过渡月份增加费用,我们建议您尽可能在月初切换到生产环境中的新 API。这样可以确保您在迁移月份达到最具成本效益的每月价格层级。如需了解价格层级,请参阅价格页面和价格常见问题解答。
启用 Places API
Place 类依赖于 Places API 服务。如需使用新地点类的功能,您必须先在 Google Cloud 项目中启用 Places API(新)。如需了解详情,请参阅使用入门。
大致变化
下表列出了 PlacesService
和 Place
之间的一些主要区别:
特定于 API 的更改
Place 类提供了一个 API,用于使用地点库,还支持 promise 等新型使用模式。Place 类公开与旧版地点服务相同的地点数据字段和地点类型,并包含许多地点数据字段和地点类型的新值。
下表显示了地点服务的功能如何映射到地点类的功能:
加载地点库
应用加载 Places 库的方式取决于所使用的引导加载器。如果您的应用使用动态库导入,您可以使用 await
运算符调用 importLibrary()
,以便在运行时加载所需的库,如下所示:
const { Place } = await google.maps.importLibrary("places");
如果您的应用使用直接脚本加载标记,请在加载器脚本中请求 places
库:
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&libraries=places&callback=initMap">
</script>
详细了解如何加载 Maps JavaScript API。
本部分包含以下指南,可帮助您迁移应用以使用最新版本的 Places API:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-02-28。
[null,null,["最后更新时间 (UTC):2025-02-28。"],[[["This guide details the transition from the legacy `PlacesService` to the new `Place` class within the Places API."],["The `Place` class offers advantages like enhanced performance, a revised pricing structure, and updated functionalities."],["Key distinctions include the use of Promises instead of callbacks, camel case for data fields, and expanded place types/data fields."],["Developers should enable the \"Places API (New)\" in their Google Cloud project and adapt their code to use the new class and methods."],["Migration guides are available to assist in transitioning specific features like Place Details, Text Search, Nearby Search, and more."]]],["The document details migrating from the legacy Places Service to the new Place class, which offers improved performance and a new pricing structure. Key changes include switching from callbacks to Promises for asynchronous operations, eliminating the need for status checks, and using camel case instead of snake case for data fields. Users must enable the Places API (New) in their Google Cloud project. The new Place class also expands the types of data and place types that are available. Additionally, the best time to switch is near the beginning of the month to take advantage of the best pricing tiers.\n"]]