2013-08-26 5 views
0

지도에서 마커가 생겼으며 어떤 마커를 클릭했는지에 따라 하나 또는 다른 마커에 애니메이션을 적용하려고했습니다. 그러나 애니메이션은 내가 만든 마지막 마커에서만 작동하며 다른 마커와는 작동하지 않습니다. 마커를 배열로 만들려고했지만 동일한 문제가 있습니다. 코드는 다음과 같습니다.다른 마커에 애니메이션 적용 API V3 Google지도

<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