Send feedback
Set collision behavior for a marker Stay organized with collections
Save and categorize content based on your preferences.
This example shows setting collision behavior for a marker.
Read the documentation
.
TypeScript
let
map
:
google.maps.Map
;
// Initialize and add the map
async
function
initMap
()
:
Promise<void>
{
// Request needed libraries.
const
{
Map
}
=
await
google
.
maps
.
importLibrary
(
"maps"
)
as
google
.
maps
.
MapsLibrary
;
const
{
AdvancedMarkerElement
}
=
await
google
.
maps
.
importLibrary
(
"marker"
)
as
google
.
maps
.
MarkerLibrary
;
let
markers
:
google.maps.marker.AdvancedMarkerElement
[]
=
[];
let
collisionBehavior
=
google
.
maps
.
CollisionBehavior
.
REQUIRED
;
map
=
new
Map
(
document
.
getElementById
(
"map"
)
as
HTMLElement
,
{
mapId
:
"6ff586e93e18149f"
,
center
:
{
lat
:
47.609414458375674
,
lng
:
-
122.33897030353548
},
zoom
:
17
,
}
as
google
.
maps
.
MapOptions
);
// @ts-ignore
const
select
=
new
mdc
.
select
.
MDCSelect
(
document
.
querySelector
(
".mdc-select"
)
as
HTMLElement
);
select
.
listen
(
"MDCSelect:change"
,
()
=
>
{
collisionBehavior
=
select
.
value
;
markers
.
forEach
((
marker
)
=
>
{
marker
.
collisionBehavior
=
collisionBehavior
;
});
});
select
.
value
=
collisionBehavior
;
// Create some markers on the map
let
locations
=
[
[
-
122.3402
,
47.6093
],
[
-
122.3402
,
47.6094
],
[
-
122.3403
,
47.6094
],
[
-
122.3384
,
47.6098
],
[
-
122.3389
,
47.6095
],
[
-
122.3396
,
47.6095
],
[
-
122.3379
,
47.6097
],
[
-
122.3378
,
47.6097
],
[
-
122.3396
,
47.6091
],
[
-
122.3383
,
47.6089
],
[
-
122.3379
,
47.6093
],
[
-
122.3381
,
47.6095
],
[
-
122.3378
,
47.6095
],
];
locations
.
forEach
(([
lng
,
lat
]
:
number
[])
=
>
{
const
advancedMarker
=
new
AdvancedMarkerElement
({
position
:
new
google
.
maps
.
LatLng
({
lat
,
lng
}),
map
,
collisionBehavior
:
collisionBehavior
,
});
markers
.
push
(
advancedMarker
);
});
}
initMap
();
Note:
Read the guide
on using TypeScript and Google Maps.
JavaScript
let
map
;
// Initialize and add the map
async
function
initMap
()
{
// Request needed libraries.
const
{
Map
}
=
await
google
.
maps
.
importLibrary
(
"maps"
);
const
{
AdvancedMarkerElement
}
=
await
google
.
maps
.
importLibrary
(
"marker"
);
let
markers
=
[];
let
collisionBehavior
=
google
.
maps
.
CollisionBehavior
.
REQUIRED
;
map
=
new
Map
(
document
.
getElementById
(
"map"
),
{
mapId
:
"6ff586e93e18149f"
,
center
:
{
lat
:
47.609414458375674
,
lng
:
-
122.33897030353548
},
zoom
:
17
,
});
// @ts-ignore
const
select
=
new
mdc
.
select
.
MDCSelect
(
document
.
querySelector
(
".mdc-select"
),
);
select
.
listen
(
"MDCSelect:change"
,
()
=
>
{
collisionBehavior
=
select
.
value
;
markers
.
forEach
((
marker
)
=
>
{
marker
.
collisionBehavior
=
collisionBehavior
;
});
});
select
.
value
=
collisionBehavior
;
// Create some markers on the map
let
locations
=
[
[
-
122.3402
,
47.6093
],
[
-
122.3402
,
47.6094
],
[
-
122.3403
,
47.6094
],
[
-
122.3384
,
47.6098
],
[
-
122.3389
,
47.6095
],
[
-
122.3396
,
47.6095
],
[
-
122.3379
,
47.6097
],
[
-
122.3378
,
47.6097
],
[
-
122.3396
,
47.6091
],
[
-
122.3383
,
47.6089
],
[
-
122.3379
,
47.6093
],
[
-
122.3381
,
47.6095
],
[
-
122.3378
,
47.6095
],
];
locations
.
forEach
(([
lng
,
lat
])
=
>
{
const
advancedMarker
=
new
AdvancedMarkerElement
({
position
:
new
google
.
maps
.
LatLng
({
lat
,
lng
}),
map
,
collisionBehavior
:
collisionBehavior
,
});
markers
.
push
(
advancedMarker
);
});
}
initMap
();
Note:
The JavaScript is compiled from the TypeScript snippet.
CSS
:
root
{
--mdc-theme-primary
:
#1a73e8
;
--mdc-theme-secondary
:
#rgb
(
225
,
245
,
254
);
--mdc-theme-on-primary
:
#fff
;
--mdc-theme-on-secondary
:
rgb
(
1
,
87
,
155
);
}
.
mdc-text-field--focused
:
not
(
.
mdc-text-field--disabled
)
.
mdc-floating-label
{
color
:
var
(
--mdc-theme-primary
);
}
.
mdc-select--focused
.
mdc-select__dropdown-icon
{
background
:
url
(
data:image/svg+xml,%3Csvg%20width%3D%2210px%22%20height%3D%225px%22%20viewBox%3D%227%2010%2010%205%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%20%20%3Cpolygon%20id%3D%22Shape%22%20stroke%3D%22none%22%20fill%3D%22%23000%22%20fill-rule%3D%22evenodd%22%20opacity%3D%220.54%22%20points%3D%227%2010%2012%2015%2017%2010%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E
)
no-repeat
center
;
}
.
mdc-select
:
not
(
.
mdc-select--disabled
)
.
mdc-select--focused
.
mdc-floating-label
{
color
:
var
(
--mdc-theme-primary
);
}
/* Optional: Makes the sample page fill the window. */
html
,
body
{
height
:
100
%
;
margin
:
0
;
padding
:
0
;
}
#
container
{
height
:
100
%
;
display
:
flex
;
}
#
sidebar
{
flex-basis
:
15
rem
;
flex-grow
:
1
;
padding
:
1
rem
;
max-width
:
30
rem
;
height
:
100
%
;
box-sizing
:
border-box
;
overflow
:
auto
;
}
#
map
{
flex-basis
:
0
;
flex-grow
:
4
;
height
:
100
%
;
}
.
mdc-select
,
.
mdc-select__anchor
,
.
mdc-select__menu
{
width
:
100
%
;
}
HTML
<html>
<head>
<title>Advanced Marker Collision Management</title>
<link
href="https://unpkg.com/material-components-web@6.0.0/dist/material-components-web.css"
rel="stylesheet"
/>
<script src="https://unpkg.com/material-components-web@6.0.0/dist/material-components-web.min.js"></script>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons"
/>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="container">
<div id="map"></div>
<div id="sidebar">
<div class="mdc-select mdc-select--outlined">
<div
class="mdc-select__anchor"
aria-labelledby="outlined-select-label"
>
<input
type="text"
disabled
readonly
id="demo-selected-text"
class="mdc-select__selected-text"
/>
<i class="mdc-select__dropdown-icon"></i>
<span class="mdc-notched-outline">
<span class="mdc-notched-outline__leading"></span>
<span class="mdc-notched-outline__notch">
<span
id="outlined-select-label"
class="mdc-floating-label mdc-theme--primary"
>Pick a Collision Behavior</span
>
</span>
<span class="mdc-notched-outline__trailing"></span>
</span>
</div>
<div class="mdc-select__menu mdc-menu mdc-menu-surface">
<ul class="mdc-list">
<li class="mdc-list-item" data-value="REQUIRED">Required</li>
<li
class="mdc-list-item"
data-value="REQUIRED_AND_HIDES_OPTIONAL"
>
Required and hides optional
</li>
<li
class="mdc-list-item"
data-value="OPTIONAL_AND_HIDES_LOWER_PRIORITY"
>
Optional and hides lower priority
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- prettier-ignore -->
<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: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "weekly"});</script>
</body>
</html>
Try Sample
Clone Sample
Git and Node.js are required to run this sample locally. Follow these instructions
to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
git
clone
-
b
sample
-
advanced
-
markers
-
collision
https
:
//github.com/googlemaps/js-samples.git
cd
js
-
samples
npm
i
npm
start
Other samples can be tried by switching to any branch beginning with sample- SAMPLE_NAME
.
git
checkout
sample
-
SAMPLE_NAME
npm
i
npm
start
Send feedback
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-09-04 UTC.
Need to tell us more?
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis example demonstrates how to manage marker collisions on a map using the \u003ccode\u003eAdvancedMarkerElement\u003c/code\u003e and \u003ccode\u003eCollisionBehavior\u003c/code\u003e properties.\u003c/p\u003e\n"],["\u003cp\u003eUsers can interactively select different collision behaviors to control how markers overlap or hide each other.\u003c/p\u003e\n"],["\u003cp\u003eThe sample code utilizes the Google Maps JavaScript API with TypeScript and JavaScript examples provided.\u003c/p\u003e\n"],["\u003cp\u003eThe code dynamically creates and positions multiple markers on the map, showcasing the impact of the selected collision behavior.\u003c/p\u003e\n"],["\u003cp\u003eA dropdown menu allows users to choose between various collision behaviors, dynamically updating the marker display.\u003c/p\u003e\n"]]],[],null,["This example shows setting collision behavior for a marker.\n\nRead the\n[documentation](/maps/documentation/javascript/advanced-markers/collision-behavior). \n\nTypeScript \n\n```typescript\nlet map: google.maps.Map;\n\n// Initialize and add the map\nasync function initMap(): Promise\u003cvoid\u003e {\n // Request needed libraries.\n const { Map } = await google.maps.importLibrary(\"maps\") as google.maps.MapsLibrary;\n const { AdvancedMarkerElement } = await google.maps.importLibrary(\"marker\") as google.maps.MarkerLibrary;\n\n let markers: google.maps.marker.AdvancedMarkerElement[] = [];\n\n let collisionBehavior = google.maps.CollisionBehavior.REQUIRED;\n\n map = new Map(\n document.getElementById(\"map\") as HTMLElement,\n {\n mapId: \"6ff586e93e18149f\",\n center: { lat: 47.609414458375674, lng: -122.33897030353548 },\n zoom: 17,\n } as google.maps.MapOptions\n );\n\n // @ts-ignore\n const select = new mdc.select.MDCSelect(\n document.querySelector(\".mdc-select\") as HTMLElement\n );\n\n\n select.listen(\"MDCSelect:change\", () =\u003e {\n collisionBehavior = select.value;\n markers.forEach((marker) =\u003e {\n marker.collisionBehavior = collisionBehavior;\n });\n });\n\n select.value = collisionBehavior;\n\n // Create some markers on the map\n let locations = [\n [-122.3402, 47.6093],\n [-122.3402, 47.6094],\n [-122.3403, 47.6094],\n [-122.3384, 47.6098],\n [-122.3389, 47.6095],\n [-122.3396, 47.6095],\n [-122.3379, 47.6097],\n [-122.3378, 47.6097],\n [-122.3396, 47.6091],\n [-122.3383, 47.6089],\n [-122.3379, 47.6093],\n [-122.3381, 47.6095],\n [-122.3378, 47.6095],\n ];\n\n locations.forEach(([lng, lat]: number[]) =\u003e {\n const advancedMarker = new AdvancedMarkerElement({\n position: new google.maps.LatLng({ lat, lng }),\n map,\n collisionBehavior: collisionBehavior,\n });\n markers.push(advancedMarker);\n });\n}\n\ninitMap();https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/samples/advanced-markers-collision/index.ts#L9-L74\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\nJavaScript \n\n```javascript\nlet map;\n\n// Initialize and add the map\nasync function initMap() {\n // Request needed libraries.\n const { Map } = await google.maps.importLibrary(\"maps\");\n const { AdvancedMarkerElement } = await google.maps.importLibrary(\"marker\");\n let markers = [];\n let collisionBehavior = google.maps.CollisionBehavior.REQUIRED;\n\n map = new Map(document.getElementById(\"map\"), {\n mapId: \"6ff586e93e18149f\",\n center: { lat: 47.609414458375674, lng: -122.33897030353548 },\n zoom: 17,\n });\n\n // @ts-ignore\n const select = new mdc.select.MDCSelect(\n document.querySelector(\".mdc-select\"),\n );\n\n select.listen(\"MDCSelect:change\", () =\u003e {\n collisionBehavior = select.value;\n markers.forEach((marker) =\u003e {\n marker.collisionBehavior = collisionBehavior;\n });\n });\n select.value = collisionBehavior;\n\n // Create some markers on the map\n let locations = [\n [-122.3402, 47.6093],\n [-122.3402, 47.6094],\n [-122.3403, 47.6094],\n [-122.3384, 47.6098],\n [-122.3389, 47.6095],\n [-122.3396, 47.6095],\n [-122.3379, 47.6097],\n [-122.3378, 47.6097],\n [-122.3396, 47.6091],\n [-122.3383, 47.6089],\n [-122.3379, 47.6093],\n [-122.3381, 47.6095],\n [-122.3378, 47.6095],\n ];\n\n locations.forEach(([lng, lat]) =\u003e {\n const advancedMarker = new AdvancedMarkerElement({\n position: new google.maps.LatLng({ lat, lng }),\n map,\n collisionBehavior: collisionBehavior,\n });\n\n markers.push(advancedMarker);\n });\n}\n\ninitMap();https://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/advanced-markers-collision/docs/index.js#L8-L67\n```\n| **Note:** The JavaScript is compiled from the TypeScript snippet.\n\nCSS \n\n```css\n:root {\n --mdc-theme-primary: #1a73e8;\n --mdc-theme-secondary: #rgb(225, 245, 254);\n --mdc-theme-on-primary: #fff;\n --mdc-theme-on-secondary: rgb(1, 87, 155);\n}\n\n.mdc-text-field--focused:not(.mdc-text-field--disabled) .mdc-floating-label {\n color: var(--mdc-theme-primary);\n}\n\n.mdc-select--focused .mdc-select__dropdown-icon {\n background: url(data:image/svg+xml,%3Csvg%20width%3D%2210px%22%20height%3D%225px%22%20viewBox%3D%227%2010%2010%205%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%0A%20%20%20%20%3Cpolygon%20id%3D%22Shape%22%20stroke%3D%22none%22%20fill%3D%22%23000%22%20fill-rule%3D%22evenodd%22%20opacity%3D%220.54%22%20points%3D%227%2010%2012%2015%2017%2010%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E) no-repeat center;\n}\n\n.mdc-select:not(.mdc-select--disabled).mdc-select--focused .mdc-floating-label {\n color: var(--mdc-theme-primary);\n}\n\n/* Optional: Makes the sample page fill the window. */\nhtml,\nbody {\n height: 100%;\n margin: 0;\n padding: 0;\n}\n\n#container {\n height: 100%;\n display: flex;\n}\n\n#sidebar {\n flex-basis: 15rem;\n flex-grow: 1;\n padding: 1rem;\n max-width: 30rem;\n height: 100%;\n box-sizing: border-box;\n overflow: auto;\n}\n\n#map {\n flex-basis: 0;\n flex-grow: 4;\n height: 100%;\n}\n\n.mdc-select,\n.mdc-select__anchor,\n.mdc-select__menu {\n width: 100%;\n}\nhttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/advanced-markers-collision/docs/style.css#L7-L60\n```\n\nHTML \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eAdvanced Marker Collision Management\u003c/title\u003e\n\n \u003clink\n href=\"https://unpkg.com/material-components-web@6.0.0/dist/material-components-web.css\"\n rel=\"stylesheet\"\n /\u003e\n \u003cscript src=\"https://unpkg.com/material-components-web@6.0.0/dist/material-components-web.min.js\"\u003e\u003c/script\u003e\n \u003clink\n rel=\"stylesheet\"\n href=\"https://fonts.googleapis.com/icon?family=Material+Icons\"\n /\u003e\n\n \u003clink rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" /\u003e\n \u003cscript type=\"module\" src=\"./index.js\"\u003e\u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv id=\"container\"\u003e\n \u003cdiv id=\"map\"\u003e\u003c/div\u003e\n \u003cdiv id=\"sidebar\"\u003e\n \u003cdiv class=\"mdc-select mdc-select--outlined\"\u003e\n \u003cdiv\n class=\"mdc-select__anchor\"\n aria-labelledby=\"outlined-select-label\"\n \u003e\n \u003cinput\n type=\"text\"\n disabled\n readonly\n id=\"demo-selected-text\"\n class=\"mdc-select__selected-text\"\n /\u003e\n \u003ci class=\"mdc-select__dropdown-icon\"\u003e\u003c/i\u003e\n \u003cspan class=\"mdc-notched-outline\"\u003e\n \u003cspan class=\"mdc-notched-outline__leading\"\u003e\u003c/span\u003e\n \u003cspan class=\"mdc-notched-outline__notch\"\u003e\n \u003cspan\n id=\"outlined-select-label\"\n class=\"mdc-floating-label mdc-theme--primary\"\n \u003ePick a Collision Behavior\u003c/span\n \u003e\n \u003c/span\u003e\n \u003cspan class=\"mdc-notched-outline__trailing\"\u003e\u003c/span\u003e\n \u003c/span\u003e\n \u003c/div\u003e\n \u003cdiv class=\"mdc-select__menu mdc-menu mdc-menu-surface\"\u003e\n \u003cul class=\"mdc-list\"\u003e\n \u003cli class=\"mdc-list-item\" data-value=\"REQUIRED\"\u003eRequired\u003c/li\u003e\n \u003cli\n class=\"mdc-list-item\"\n data-value=\"REQUIRED_AND_HIDES_OPTIONAL\"\n \u003e\n Required and hides optional\n \u003c/li\u003e\n \u003cli\n class=\"mdc-list-item\"\n data-value=\"OPTIONAL_AND_HIDES_LOWER_PRIORITY\"\n \u003e\n Optional and hides lower priority\n \u003c/li\u003e\n \u003c/ul\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n\n \u003c!-- prettier-ignore --\u003e\n \u003cscript\u003e(g=\u003e{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=()=\u003eh||(h=new Promise(async(f,n)=\u003e{await (a=m.createElement(\"script\"));e.set(\"libraries\",[...r]+\"\");for(k in g)e.set(k.replace(/[A-Z]/g,t=\u003e\"_\"+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=()=\u003eh=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)=\u003er.add(f)&&u().then(()=\u003ed[l](f,...n))})\n ({key: \"AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg\", v: \"weekly\"});\u003c/script\u003e\n \u003c/body\u003e\n\u003c/html\u003ehttps://github.com/googlemaps/js-samples/blob/2683f7366fb27829401945d2a7e27d77ed2df8e5/dist/samples/advanced-markers-collision/docs/index.html#L8-L79\n```\n\nTry Sample \n[JSFiddle.net](https://jsfiddle.net/gh/get/library/pure/googlemaps/js-samples/tree/master/dist/samples/advanced-markers-collision/jsfiddle) [Google Cloud Shell](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fgooglemaps%2Fjs-samples&cloudshell_git_branch=sample-advanced-markers-collision&cloudshell_tutorial=cloud_shell_instructions.md&cloudshell_workspace=.)\n\nClone Sample\n\n\nGit and Node.js are required to run this sample locally. Follow these [instructions](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to install Node.js and NPM. The following commands clone, install dependencies and start the sample application. \n\n git clone -b sample-advanced-markers-collision https://github.com/googlemaps/js-samples.git\n cd js-samples\n npm i\n npm start\n\n\nOther samples can be tried by switching to any branch beginning with `sample-`\u003cvar translate=\"no\"\u003eSAMPLE_NAME\u003c/var\u003e. \n\n git checkout sample-\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-nx\"\u003eSAMPLE_NAME\u003c/span\u003e\u003c/var\u003e\n npm i\n npm start"]]