2

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

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

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

4

1 回答 1

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

    });
}

您之前的代码只查看刚刚单击的标记 - 如果它没有单击(开始状态),那么您会使其反弹。下一次单击检查它是否正在弹跳(它是),并停止它。如果您想再次单击以停止弹跳,您可以将相同的逻辑添加到上面的代码中。

于 2012-09-22T15:09:58.833 回答