Marker Collision Management

Handle marker collisions

Collision behavior controls how a marker will display if it collides (overlaps) with another marker. Collision behavior is only supported when using Advanced Markers on a vector map.

To set collision behavior, set AdvancedMarkerView.collisionBehavior to one of the following:

  • REQUIRED: (default) Always display the marker regardless of collision.
  • OPTIONAL_AND_HIDES_LOWER_PRIORITY Display the marker only if it does not overlap with other markers. If two markers of this type would overlap, the one with the higher zIndex is shown. If they have the same zIndex, the one with the lower vertical screen position is shown.
  • REQUIRED_AND_HIDES_OPTIONAL Always display the marker regardless of collision, and hide any OPTIONAL_AND_HIDES_LOWER_PRIORITY markers or labels that would overlap with the marker.

The following example always displays markers and hides underlying labels:

const advancedMarker = new google.maps.marker.AdvancedMarkerView({
  position: new google.maps.LatLng({ lat, lng }),
  map,
  collisionBehavior: google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL,
});

The following example hides markers if they collide with a required marker and hides underlying labels:

const advancedMarker = new google.maps.marker.AdvancedMarkerView({
  position: new google.maps.LatLng({ lat, lng }),
  map,
  collisionBehavior: google.maps.CollisionBehavior.OPTIONAL_AND_HIDES_LOWER_PRIORITY,
});

TypeScript

let map: google.maps.Map;

// Initialize and add the map
async function initMap(): Promise {
  // 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();

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();

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: 15rem;
  flex-grow: 1;
  padding: 1rem;
  max-width: 30rem;
  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


  
    Advanced Marker Collision Management

    
    
    

    
    
  
  
    

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