Google Maps API - 弹跳标记问题

tri*_*con 2 javascript google-maps google-maps-api-3

选择标记后,我希望它反弹.当我点击另一个标记时,我希望第一个停止弹跳,然后另一个开始弹跳.我认为这可以通过简单的方法实现

function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
 document.getElementById('loc-info').innerHTML = html;
 if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
});
}
Run Code Online (Sandbox Code Playgroud)

相反,第一个标记将继续反弹,直到再次单击它为止,我不想发生这种情况.有什么想法吗?

dou*_*arp 5

您需要跟踪"当前"标记并将其动画设置为null,然后再弹出新标记并将其设置为"当前"标记.

// track marker that is currently bouncing
var currentMarker = null;

function bindInfoWindow(marker, map, infoWindow, html) {
    google.maps.event.addListener(marker, 'click', function() {
        document.getElementById('loc-info').innerHTML = html;
        // remove the bounce from the "old" marker
        if (currentMarker) currentMarker.setAnimation(null);
        // set this marker to the currentMarker
        currentMarker = marker;
        // add a bounce to this marker
        marker.setAnimation(google.maps.Animation.BOUNCE);

    });
}
Run Code Online (Sandbox Code Playgroud)

您之前的代码只查看刚刚单击的标记 - 如果它没有单击(开始状态),那么您可以使其反弹.下一次单击检查它是否正在弹跳(它是),然后停止它.如果您希望第二次单击停止弹跳,可以在上面的代码中添加相同的逻辑.