Animate a marker using CSS _ Maps JavaScript API _ Google for Developers-
Animate a marker using CSS _ Maps JavaScript API _ Google for Developers-
7:48 Animate a marker using CSS | Maps JavaScript API | Google for Developers
This example creates the traditional "bounce-drop" animation using CSS and advanced
markers. In the IntersectionObserver, it adds the drop CSS style. The
IntersectionObserversees when each marker enters the viewport, and adds the style.
Then, the animationend event listener that the createMarker() function added to each
marker removes the style.
/**
* Returns a random lat lng position within the map bounds.
* @param {!google.maps.Map} map
* @return {!google.maps.LatLngLiteral}
*/
function getRandomPosition(map) {
const bounds = map.getBounds();
const minLat = bounds.getSouthWest().lat();
const minLng = bounds.getSouthWest().lng();
const maxLat = bounds.getNorthEast().lat();
https://developers.google.com/maps/documentation/javascript/examples/advanced-markers-animation 1/4
2025. 05. 23. 7:48 Animate a marker using CSS | Maps JavaScript API | Google for Developers
https://developers.google.com/maps/documentation/javascript/examples/advanced-markers-animation 2/4
2025. 05. 23. 7:48 Animate a marker using CSS | Maps JavaScript API | Google for Developers
controlDiv.appendChild(controlUI);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(controlDiv
}
function createMarker(map, AdvancedMarkerElement, PinElement) {
const pinElement = new PinElement();
const content = pinElement.element;
const advancedMarker = new AdvancedMarkerElement({
position: getRandomPosition(map),
map: map,
content: content,
});
content.style.opacity = '0';
content.addEventListener('animationend', (event) => {
content.classList.remove('drop');
content.style.opacity = '1';
});
const time = 2 + Math.random(); // 2s delay for easy to see the anim
content.style.setProperty('--delay-time', time + 's');
intersectionObserver.observe(content);
}
function refreshMap() {
// Refresh the map.
const mapContainer = document.getElementById('mapContainer');
const map = document.getElementById('map');
map.remove();
const mapDiv = document.createElement('div');
mapDiv.id = 'map';
mapContainer.appendChild(mapDiv);
initMap();
}
initMap();
96ae2638d3fb4dfe506bbb6f/dist/samples/advanced-markers-animation/docs/index.js#L8-L96)
Try Sample
JSFiddle.net…
Clone Sample
Git 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.
https://developers.google.com/maps/documentation/javascript/examples/advanced-markers-animation 3/4
2025. 05. 23. 7:48 Animate a marker using CSS | Maps JavaScript API | Google for Developers
The following commands clone, install dependencies and start the sample application.
https://developers.google.com/maps/documentation/javascript/examples/advanced-markers-animation 4/4