I want to bounce a pin on a google map once. The following code will make the marker bounce but it just keeps going...
myPin.setAnimation(google.maps.Animation.BOUNCE);
Then calling
myPin.setAnimation(null);
makes the animation stop. Setting a timeout works but the duration of a bounce doesn't look like it is a round number so doing this
setTimeout(function(){ myPin.setAnimation(null); }, 1000);
Make the bounce animation end prematurely and look terrible.
Does anyone know of a better way to accomplish this?
I have found that in order for a pin to stop animating after a bounce is completed, you need to make the pin draggable. The best way I have found to do this is by using two timeouts:
Animations will stop once you make a marker not draggable. I have created a plunker to show what I mean: http://plnkr.co/edit/Gcns3DMklly6UoEJ63FP?p=preview
The HTML
The JavaScript
For as far as I am aware this works cross browser. I have tested in chrome, firefox, safari & opera. I haven't tested this in internet explorer yet.
This is a tough one with no perfect answer yet because, while 750ms works fine for a desktop browser, it does look stunted on a mobile device. Google has not really added much to the animation API, so there's no solutions via API.
The best I've been able to accomplish is to set the timeout value to 900ms, it looks the same on Desktop because it exploits the 150ish ms that the icon pauses between each bounce, and gives a laggy mobile device some extra breathing space for animation time.Edit: My solution stopped working for me suddenly. Oops. If you're doing this on mobile, might just be best to not bother with the bounce at all.
Okay none of the other answers quite worked well enough given the limitations of the API. So here's what I've found.
js?v=3.13
.marker.setAnimation(null)
stops the marker from bouncing only after it completes the current bounce. So if you wait until 710ms have expired in the bounce sequence and then setmarker.setAnimation(null)
, the API will continue to perform an additional bounce without interrupting its current bounce sequence.setAnimation(700)
again on that same marker it will interrupt the current bounce sequence. Not exactly pretty.Simple case (as seen in the other answers):
Assuming that:
you can use
setTimout
in conjunction with jquery's.queue
method to prevent a new bounce request from interrupting the current one, but still queuing it up to perform the bounce sequence after the current one completes. (note: I used two bounces instead of one so msec is set to 1400).more realistic case: