最佳做法
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
JS 最佳做法
Maps JavaScript API 僅適用於標準 ECMAScript 和 W3C DOM 環境。這表示修改或覆寫瀏覽器所提供內建類別和物件的行為,可能會導致 Maps JavaScript API 無法運作。其他程式庫有時可能會變更瀏覽器行為,與 Maps JavaScript API 發生衝突,導致瀏覽器不再是標準 ECMAScript 環境。Maps JavaScript API 與這些程式庫不相容。
已知與 Maps JavaScript API 不相容的程式庫:
- Prototype:以非標準方式覆寫
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 不支援變更使用者代理程式樣式表。如果架構或元件參照 CSS 類別或 Maps JavaScript API 的 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
,建議您嘗試下列做法:
- 建立
box-sizing
覆寫,將 <html>
元素設為 border-box
。
- 為您地圖以外的所有元素使用
box-sizing: inherit
。
- 建立自訂地圖容器,將
box-sizing
元素重設為 initial
。
CSS 範例:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
.container-map {
box-sizing: initial;
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[null,null,["上次更新時間: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 }"]]