Я обнаружил, что для прекращения анимации булавки после того, как отскок завершен, вам нужно сделать булавку перетаскиваемой. Лучший способ сделать это - использовать два таймаута:
- для удаления анимации до завершения первого отскока.
- чтобы маркер не перетаскивался после завершения первого отскока.
Анимации прекратятся, как только вы сделаете маркер не перетаскиваемым. Я создал вантуз, чтобы показать, что я имею в виду: http://plnkr.co/edit/Gcns3DMklly6UoEJ63FP?p=preview
HTML
<div id="map-canvas"></div>
<p>
Bounce marker
<select id="bounceNumber">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
times.
<button onclick="bounceMarker()">Go!</button>
</p>
JavaScript
var marker = null,
toBounce = null,
toDrag = null;
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(-25.363882, 131.044922)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
marker = new google.maps.Marker({
position: new google.maps.LatLng(-25.363882, 131.044922),
map: map
});
}
function bounceMarker() {
var select = document.getElementById("bounceNumber"),
bounces = parseInt(select.options[select.selectedIndex].value),
animationTO = (bounces - 1) * 700 + 350,
dragTO = animationTO + 1000;
// Bounce the marker once
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
clearTimeout(toBounce);
clearTimeout(toDrag);
setTimeout(function () {
marker.setDraggable(false);
}, 750);
} else {
// Workaround to make marker bounce once.
// The api will finish the current bounce if a marker is set to draggable.
// So use two timeouts:
// 1. to remove the animation before the first bounce is complete.
// 2. to make the marker not draggable after the first bounce is complete.
// Animations will stop once you make a marker not draggable.
marker.setDraggable(true);
marker.setAnimation(google.maps.Animation.BOUNCE);
toBounce = setTimeout(function () {
marker.setAnimation(null);
}, animationTO);
toDrag = setTimeout(function () {
marker.setDraggable(false);
}, dragTO);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
Насколько я знаю, это работает кросс-браузер. Я тестировал в Chrome, Firefox, Safari & Opera. Я еще не проверял это в Internet Explorer.