我的 map 中有一些标记,我想为一个或另一个标记设置动画,具体取决于我“单击”的标记。但动画只适用于我创建的最后一个标记,而不适用于其他标记。 我尝试将标记制作为数组,但遇到了同样的问题。 这是代码:
<script type="text/javascript">
var marker = null;
var address = null;
var altura = null;
function codeAddress() {
altura = document.getElementById('altura').value;
address = document.getElementById('address').value + ' ' + altura ;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
// Marker
marker = new google.maps.Marker({
map: map,
icon: document.getElementById('icono').value,
position: results[0].geometry.location,
animation: google.maps.Animation.DROP
});
// Animating Listener
google.maps.event.addListener(marker, 'click', toggleBounce);
} else {
alert('Error: ' + status);
}
});
}
function toggleBounce() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
</script>
提前致谢。
最佳答案
您需要保留对所有要设置动画的标记的引用,然后在正确的标记上设置动画。您发布的代码只有一个标记。
解决问题的一种方法是使用函数闭包:
function createMarker(latlng, address, icon){
// Marker
var marker = new google.maps.Marker({
map: map,
icon: icon,
position: latlng,
animation: google.maps.Animation.DROP
});
// Animating Listener
google.maps.event.addListener(marker, 'click', function() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
});
}
关于javascript - 动画不同标记 API V3 Google map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18436599/