通知:Google Maps Platform 即将推出全新基本地图样式。此次地图样式更新包括全新默认调色板、经过现代化改进的图钉,以及地图体验和易用性方面的改进。所有地图样式将于 2025 年 3 月自动更新。如需详细了解适用范围以及如何提前选择启用,请参阅
全新 Google Maps Platform 地图样式。
开始使用
此页面介绍了如何获取 API 密钥、启用所需的 API 以及加载地点库,以便您使用 Place 类。
获取 API 密钥并启用所需的 API
在使用 Place 类之前,您需要:
- 创建有结算账号的 Cloud 项目。
- 获取 API 密钥。
- 启用以下 API:
- Maps JavaScript API
- Places API
- Places API(新)(必须启用此 API 才能使用文本搜索[新])
如需了解详情,请参阅设置您的 Google Cloud 项目。
获取 API 密钥
启用 Places API(新)
加载地点库
若要加载地点库,请先通过将内嵌引导程序加载器添加到应用代码中来加载 Maps JavaScript API,如以下代码段所示:
<script>
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "YOUR_API_KEY",
v: "weekly",
// Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.).
// Add other bootstrap parameters as needed, using camel case.
});
</script>
接下来,使用 await
运算符从 async
函数中调用 importLibrary()
,如下所示:
const {Place} = await google.maps.importLibrary("places");
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-01-14。
[null,null,["最后更新时间 (UTC):2025-01-14。"],[[["This guide explains how to prepare your application to work with the Google Maps Places API's Place class."],["You'll need to set up a Cloud project, obtain an API key, and enable necessary APIs including Maps JavaScript API, Places API, and Places API (New)."],["The Places Library is loaded by including the Maps JavaScript API bootstrap loader and then calling `importLibrary(\"places\")`."],["Once set up, you can utilize the Place class for searching places, retrieving details, and using Place Autocomplete."]]],["To use the Place class, you must first create a Cloud project with billing, obtain an API key, and enable the Maps JavaScript API, Places API, and Places API (New). Then, load the Places Library by including the Maps JavaScript API bootstrap loader in your code, and utilizing the `importLibrary()` function within an `async` context. This allows for the use of the \"places\" library, and subsequent place related tasks.\n"]]