2013-08-26 49 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