2013-08-26 45 views
0

我在地图上有一些标记,我想根据哪一个做出“点击”来制作一个或另一个标记。但动画只与我创建的最后一个标记一起工作,而不与其他人创建。 我试着将标记作为数组,但是是同样的问题。 这里是代码:动画不同标记API V3谷歌地图

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

在此先感谢。

回答

2

您需要保留所有想要制作动画的标记的引用,然后将动画设置为正确的动画。您发布的代码只有一个标记。解决你的问题的

一种方式是使用函数闭:

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

working example

+0

感谢您的答复。 它正在制造相同的问题。 我做了一个新的标记,当我点击它是好的,但是当我做一个新的,我不能做与以前的行动。这是新的改编的代码:http://pastebin.com/Puie9sW7 – Chapo58

+0

你看了一个实际的例子吗?它为我工作。 – geocodezip

+0

您的代码与上面发布的代码不同。它在标记之前缺少“var”,仍然只留下一个标记。 – geocodezip