迁移:google.load 中的 Maps 模块
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
我们将于 2021 年 10 月 13 日停用为 google.load
提供“Maps”模块的服务。这意味着,在 2021 年 10 月 13 日之后,如果您尝试在 google.load
中使用“Maps”模块,则会收到错误消息(模块“Maps”不受支持),并且系统不会加载任何地图。为避免出现可能的中断,您必须改用某种替代方案。
我需要做什么?
首先,移除用于加载 google.load
加载器的 <script>
标记,然后移除对 google.load
的调用。如果您要使用 Google 加载器执行其他操作,则可以保留加载器 <script>
标记。
接下来,实施一种新方法来加载 Maps JavaScript API(请选择以下选项之一):
使用 Google 加载器的当前示例
以下示例展示了当前使用 Google 加载器加载 Maps JavaScript API 的方式(有两个 <script>
块):
之前
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load("maps", "3.exp", {
"callback": initMap,
"key": "YOUR_KEY",
"libraries": "places,visualization"
});
function initMap() {
// Google Maps JS API is loaded and available
}
</script>
使用 <script>
标记以内嵌方式加载(推荐)
使用此方法时,Maps JavaScript API 会与网页同时加载。若要实现以内嵌方式加载,请先将用于加载 www.google.com/jsapi 的 <script>
标记(请参阅“之前”部分)替换为下例中所示的 <script>
标记:
<script async src="https://maps.googleapis.com/maps/api/js?libraries=places,visualization&key=YOUR_API_KEY&v=weekly&callback=initMap">
</script>
然后,由于不再需要 google.load
函数调用,请从 JavaScript 代码中将其移除。以下示例展示了一个空 initMap()
函数,该函数在地图库成功加载时调用:
<script type='text/javascript'>
function initMap() {
// Google Maps JS API is loaded and available
}
</script>
请参阅相关文档
从其他 JavaScript 文件动态加载
通过动态加载,您可以控制何时加载 Maps JavaScript API。例如,您可以等到用户点击某个按钮或执行其他操作时再加载 Maps JavaScript API。若要实现动态加载,请先将用于加载 www.google.com/jsapi 的 <script>
标记(请参阅“之前”部分)替换为用于以程序化方式添加 <script>
标记的代码,如以下示例所示:
var script = document.createElement('script');
script.src =
'https://maps.googleapis.com/maps/api/js?libraries=places,visualization&key=YOUR_API_KEY&v=weekly&callback=initMap';
script.async=true;
然后,将您的回调函数附加到窗口对象,如下所示:
window.initMap = function() {
// Google Maps JS API is loaded and available
};
最后,将 <script>
标记添加到页面标头中,如下所示:
document.head.appendChild(script);
请参阅相关文档
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eThe "Maps" module for \u003ccode\u003egoogle.load\u003c/code\u003e will be discontinued on October 13, 2021, resulting in map loading errors if still in use.\u003c/p\u003e\n"],["\u003cp\u003eTo avoid issues, developers must switch to alternative loading methods like inline or dynamic loading before the deadline.\u003c/p\u003e\n"],["\u003cp\u003eInline loading involves replacing the \u003ccode\u003egoogle.load\u003c/code\u003e script with a new script tag directly embedding the Maps JavaScript API.\u003c/p\u003e\n"],["\u003cp\u003eDynamic loading allows for controlled, on-demand loading of the Maps JavaScript API triggered by user actions or specific events.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers should remove or update the existing \u003ccode\u003egoogle.load\u003c/code\u003e function calls in their code after implementing the new loading method.\u003c/p\u003e\n"]]],[],null,["# Migration: Maps Module in google.load\n\nOn October 13, 2021, we will turn off the service that provides the \"Maps\"\nmodule for [`google.load`](https://support.google.com/code/answer/74501).\nThis means that after October 13, 2021, if you try to use the \"Maps\" module in\n`google.load` you will receive an error (*module \"maps\" is not supported*), and\nno map will load. To help you avoid potential breakage, you must switch to one\nof the alternatives.\n\nWhat do I need to do?\n---------------------\n\nFirst, remove the `\u003cscript\u003e` tag that loads the `google.load` loader,\nthen remove calls to `google.load`. If you're using Google Loader for other\nthings, it's okay to leave the loader `\u003cscript\u003e` tag in place.\n\nNext, implement a new way to load the Maps JavaScript API (select\none of the following options):\n\n- [Inline loading using the `\u003cscript\u003e` tag](#inline-loading)\n- [Dynamic loading from another JavaScript file](#dynamic-loading)\n\n### Current example using the Google Loader\n\nThe following example shows how the Google Loader is currently used to load the\nMaps JavaScript API (there are two `\u003cscript\u003e` blocks):\n\n#### Before\n\n \u003cscript type='text/javascript' src='https://www.google.com/jsapi'\u003e\u003c/script\u003e\n \u003cscript type='text/javascript'\u003e\n google.load(\"maps\", \"3.exp\", {\n \"callback\": initMap,\n \"key\": \"YOUR_KEY\",\n \"libraries\": \"places,visualization\"\n });\n function initMap() {\n // Google Maps JS API is loaded and available\n }\n \u003c/script\u003e\n\n### Inline loading using the `\u003cscript\u003e` tag (recommended)\n\nWhen this approach is used, the Maps JavaScript API loads at the\nsame time the page loads. To implement inline loading, first replace the\n`\u003cscript\u003e` tag that loads www.google.com/jsapi ([\"before\"](#before)) with the\n`\u003cscript\u003e` tag shown in the following example: \n\n \u003cscript async src=\"https://maps.googleapis.com/maps/api/js?libraries=places,visualization&key=YOUR_API_KEY&v=weekly&callback=initMap\"\u003e\n \u003c/script\u003e\n\nThen in your javascript code, remove the `google.load` function call, since\nit's no longer needed. The following example shows a blank `initMap()`\nfunction, which is called when the Maps library has loaded successfully: \n\n \u003cscript type='text/javascript'\u003e\n function initMap() {\n // Google Maps JS API is loaded and available\n }\n \u003c/script\u003e\n\n[See the documentation](/maps/documentation/javascript/overview#Loading_the_Maps_API)\n\n### Dynamic loading from another JavaScript file\n\nDynamic loading lets you control when the Maps JavaScript API is loaded. For\nexample, you can wait to load the Maps JavaScript API until the\nuser clicks a button or performs another action. To implement dynamic loading,\nfirst replace the `\u003cscript\u003e` tag that loads www.google.com/jsapi ([\"before\"](#before))\nwith code to programmatically add the `\u003cscript\u003e` tag, as shown in the following example: \n\n var script = document.createElement('script');\n script.src =\n 'https://maps.googleapis.com/maps/api/js?libraries=places,visualization&key=YOUR_API_KEY&v=weekly&callback=initMap';\n script.async=true;\n\nThen attach your callback function to the window object like this: \n\n window.initMap = function() {\n // Google Maps JS API is loaded and available\n };\n\nFinally, add the `\u003cscript\u003e` tag to the header of the page like this: \n\n document.head.appendChild(script);\n\n[See the documentation](/maps/documentation/javascript/overview#Loading_the_Maps_API)"]]