Google 地图 API - 弹跳标记问题

2024-01-04

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

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

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


您需要跟踪“当前”标记并将其动画设置为 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);

    });
}

您之前的代码仅查看刚刚单击的标记 - 如果它没有单击(开始状态),那么您将使其弹跳。下一次单击检查它是否弹跳(确实弹跳),然后停止它。如果您想要第二次单击来停止弹跳,您可以将相同的逻辑添加到上面的代码中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 地图 API - 弹跳标记问题 的相关文章

随机推荐