简体   繁体   English

Google Maps API v3标记拖放动画

[英]Google Maps API v3 marker drop and bounce animation

I got this modified example code from google 我从谷歌那里得到了修改后的示例代码

var stockholm = new google.maps.LatLng(59.32522, 18.07002);
var parliament = new google.maps.LatLng(59.327383, 18.06747);
var marker;
var map;

function initialize() {
    var mapOptions = {
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: stockholm
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    marker = new google.maps.Marker({
        map:map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        position: parliament,
        icon: '/img/marker.png'
    });
    google.maps.event.addListener(marker, 'click', toggleBounce);


    setTimeout(function() {  marker.setAnimation(google.maps.Animation.BOUNCE); }, 2000);

}

function toggleBounce() {

  if (marker.getAnimation() != null) {
    marker.setAnimation(null);
  } else {
    marker.setAnimation(google.maps.Animation.BOUNCE);
  }
}

and I wonder if it is possible to change marker animation from DROP to BOUNCE after DROP animation stops? 并且我想知道是否可以在DROP动画停止后将标记动画从DROP更改为BOUNCE?

I managed to change it using setTimeout() function but it does not do it smoothly. 我设法使用setTimeout()函数对其进行了更改,但它无法顺利进行。 Any help will be appreciated. 任何帮助将不胜感激。

Try changing the marker animation on google.maps.event.addListener(map, 'idle', function ()...) - this will be called after the marker(s) are added. 尝试更改google.maps.event.addListener(map, 'idle', function ()...)上的标记动画-添加标记后将调用此动画。

 document.write('<script src="https://maps.googleapis.com/maps/api/js">\\x3C/script>'); window.onload = function () { // Create map var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 12, center: new google.maps.LatLng(-33.87, 151.24), mapTypeControlOptions: { mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style'] } }); // Create marker var marker = new google.maps.Marker({ position: new google.maps.LatLng(-33.890542, 151.274856), map: map, animation: google.maps.Animation.DROP, title: 'Bondi Beach' }); // On idle, change marker animation to bounce google.maps.event.addListener(map, 'idle', function () { marker.setAnimation(google.maps.Animation.BOUNCE); }); } 
 #map_canvas { width: 300px; height: 300px; } 
 <div id="map_canvas"></div> 

You could try this. 你可以试试看。 :

google.maps.event.addListener(marker, "dragend", function(event) { 
          marker.setAnimation(google.maps.Animation.BOUNCE);
        }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM