最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
JS 最佳实践
Maps JavaScript API 仅适用于标准的 ECMAScript 和 W3C DOM 环境。这意味着,如果修改或替换浏览器所提供的内置类和对象的行为,则可能会导致 Maps JavaScript API 无法正常运行。有时,其他库可能会与 Maps JavaScript API 冲突,它们会更改浏览器的行为,使其不再是标准的 ECMAScript 环境。Maps JavaScript API 与这些库不兼容。
已知与 Maps JavaScript API 不兼容的库:
- 原型:以非标准的方式替换
Array.from()
和 Element.prototype.remove()
。
- MooTools(旧版):以非标准的方式替换
Array.from()
。
- DateJS(旧版):以非标准的方式替换
Date.now()
。
有时,您可以通过修改不兼容的库来移除非标准的替换项。
CSS 最佳实践
当您使用 Maps JavaScript API 添加或自定义地图时,应用于网页的部分样式可能会替换您的地图样式,造成 CSS 冲突。如果您使用 CSS 框架或 JavaScript 组件来设置样式,这可能会导致 CSS 与您的地图样式发生更多其他冲突。
CSS 框架和 JavaScript 样式设置组件通常会使用 CSS 重置或标准化程序来处理浏览器之间的渲染差异。框架往往会使用 box-sizing
元素来调整网页元素的外边距和边框。这通常需要将默认浏览器行为从使用 content-box
更改为使用 border-box
。
这种类型的 CSS 重置可能会导致 CSS 与 Maps JavaScript API 发生冲突,因为该 API 不支持对用户代理样式表进行更改。如果框架或组件引用了 Maps JavaScript API 的 CSS 类或 DOM 元素,则可能会发生其他 CSS 冲突。
为避免这些冲突,我们提供了几项建议供您参考。
特异性
嵌入和关联的 CSS 会先于 Google 地图样式应用于您的地图。如果您的所有网页样式均在嵌入或关联的 CSS 中定义,请遵循特异性规则,以确保应用于地图的样式正确无误。
您的网页样式可能会覆盖常见的 CSS 元素,例如 img
、button
和 a
。当 img
元素的 max-width
属性设置为 100% 时最常发生这种情况。这可能会导致地图组件失真或隐藏起来,尤其是在您使用 InfoWindow 时。
若要解决此问题,您可以更新地图的 img
元素,将 max-width
属性设置为 none
。例如:
#map img
{
max-width : none;
}
类名称
请勿引用 JavaScript Maps API 的类名称和内部 DOM 元素。这种做法不受支持,并可能会导致您的网站中发生破坏性更改而不会发出通知。建议您改为自行创建 CSS 类作为地图的容器。
如果 CSS 框架或 JavaScript 组件使用这些类型的引用,我们建议的 box-sizing 替换做法或许可作为一种权宜之计。
CSS box-sizing 替换
CSS box-sizing 替换做法或许是可解决地图样式设置冲突的一种权宜之计。如果您使用的是 CSS 框架或 JavaScript 样式设置组件,这种方法尤为有用。例如,如果 box-sizing
设置为 border-box
,请尝试以下操作:
- 创建一项将
<html>
元素设置为 border-box
的 box-sizing
替换操作。
- 为您的地图以外的所有元素使用
box-sizing: inherit
。
- 创建一个将
box-sizing
元素重置为 initial
的自定义地图容器。
CSS 示例:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
.container-map {
box-sizing: initial;
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eAvoid using JavaScript libraries like Prototype, MooTools (older versions), and DateJS (older versions) as they can conflict with the Maps JavaScript API due to their non-standard overrides of built-in browser functionalities.\u003c/p\u003e\n"],["\u003cp\u003ePrevent CSS conflicts by following specificity rules and avoiding direct references to Maps JavaScript API class names or internal DOM elements; instead, use custom CSS classes as containers for your map.\u003c/p\u003e\n"],["\u003cp\u003eIf using CSS frameworks or components that cause conflicts, consider a box-sizing override: set the \u003ccode\u003e<html>\u003c/code\u003e element to \u003ccode\u003eborder-box\u003c/code\u003e, use \u003ccode\u003ebox-sizing: inherit\u003c/code\u003e for other elements, and reset \u003ccode\u003ebox-sizing\u003c/code\u003e to \u003ccode\u003einitial\u003c/code\u003e for your custom map container.\u003c/p\u003e\n"],["\u003cp\u003eCommon CSS elements like \u003ccode\u003eimg\u003c/code\u003e, \u003ccode\u003ebutton\u003c/code\u003e, and \u003ccode\u003ea\u003c/code\u003e can be overwritten by page styles, potentially causing distorted or hidden map components; adjust styles accordingly, for example, setting \u003ccode\u003emax-width: none\u003c/code\u003e for map images to avoid issues.\u003c/p\u003e\n"]]],["The Maps JavaScript API may conflict with other libraries that modify browser behavior, notably Prototype, older MooTools, and older DateJS versions. CSS conflicts arise when page styles, frameworks, or JavaScript components override map styles. To prevent issues, use CSS specificity rules, avoid referencing internal API class names, and update elements like `img` to `max-width: none`. A workaround for `box-sizing` conflicts is to set `\u003chtml\u003e` to `border-box`, other elements to `inherit`, and map containers to `initial`.\n"],null,["JS Best Practices\n\nThe Maps JavaScript API only works with a standard ECMAScript and W3C DOM\nenvironment. This means that modifying or overriding the behavior of the built-in\nclasses and objects provided by the browser could make the Maps JavaScript API\nnon-functional. Sometimes other libraries can conflict with the Maps JavaScript\nAPI by changing the behavior of the browser so that it is no longer a standard\nECMAScript environment. The Maps JavaScript API is not compatible with those\nlibraries.\n\nLibraries that are known to be incompatible with the Maps JavaScript API:\n\n- Prototype: overrides `Array.from()` and `Element.prototype.remove()` in non-standard ways.\n- MooTools (older versions): overrides `Array.from()` in a non-standard way.\n- DateJS (older versions): overrides `Date.now()` in a non-standard way.\n\nSometimes it is possible to modify incompatible libraries to remove the\nnon-standard overrides.\n\nCSS Best Practices\n\nWhen you add or customize a map with the Maps JavaScript API, some of the styles\nyou apply to your webpage may override your map styles and cause CSS conflicts.\nIf you use a CSS framework or a JavaScript component for styling, this might add\nadditional CSS conflicts with your map styles.\n| **Note:** To verify the styles that the browser applies to each HTML element, see [Chrome DevTools CSS Reference](https://developers.google.com/web/tools/chrome-devtools/css/reference).\n\nCSS frameworks and JavaScript styling components often use a CSS reset or a\nnormalizer to handle rendering differences between browsers. Frameworks often\nuse the `box-sizing` element to scale the margins and borders of web page\nelements. This usually involves changing the default browser behavior from using\n`content-box` to `border-box`.\n\nThis type of CSS reset can cause CSS conflicts with the Maps JavaScript API\nbecause the API doesn't support changes to the user agent stylesheet. Additional\nCSS conflicts can occur if the framework or component references CSS classes or\nDOM elements of the Maps JavaScript API.\n\nTo avoid these conflicts, we have several recommendations to consider.\n\nSpecificity\n\nEmbedded and linked CSS is applied to your map before the Google maps styles. If\nall of your page styles are defined in embedded or linked CSS, follow the\n[specificity rules](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity)\nto ensure that the correct styles are applied to your map.\n\nCommon CSS elements, such `img`, `button`, and `a` can be overwritten by the\nstyles of your page. One of the most common scenarios is when the `max-width`\nattribute of the `img` element is set to 100 percent. This can cause distorted\nor hidden map components, especially if you're using\n[InfoWindow](/maps/documentation/javascript/infowindows).\n\nTo fix this issue, you can update the `img` element for your map so the\n`max-width` attribute is set to `none`. For example: \n\n #map img\n {\n max-width : none;\n }\n\nClass names\n\nDon't reference class names and internal DOM elements of the JavaScript Maps\nAPI. This is not supported and can cause breaking changes in your website\nwithout notice. Instead, we recommend that you create your own CSS classes as\ncontainers for your map.\n\nIf a CSS framework or JavaScript component uses these type of references, our\nbox-sizing override recommendation might serve as a\nwork-around.\n\nCSS box-sizing override\n\nCSS box-sizing overrides provide a possible workaround for maps styling\nconflicts. This can be especially useful if you're using a CSS framework or\nJavaScript styling component. For example, if `box-sizing` is set to\n`border-box`, try the following:\n\n- Create a `box-sizing` override that sets the `\u003chtml\u003e` element to `border-box`.\n- Use `box-sizing: inherit` for all elements other than your map.\n- Create a custom map container that resets the `box-sizing` element to `initial`.\n\nCSS example: \n\n html {\n box-sizing: border-box;\n }\n\n *,\n *::before,\n *::after {\n box-sizing: inherit;\n }\n\n .container-map {\n box-sizing: initial;\n }"]]