当我一个接一个地放下每个标记时,如何在谷歌地图V3标记上添加自定义动画

How to add custom animation on Google Map V3 Marker when I drop each marker one by one?

本文关键字:一个 谷歌地图 V3 添加 动画 自定义 放下      更新时间:2023-09-26

这是我在谷歌地图V3上逐个删除每个标记的简单工作示例。我已经设置了将标记添加到谷歌地图时的拖放动画。

但是我想自定义带有淡入淡出动画的下降,可以使用任何Javascript东西或其他库吗?

谷歌在命名空间中有这个选项 我们可以在命名空间中添加我们的自定义动画选项吗?

  • google.maps.Animation.DROP
  • google.maps.Animation.BOUNCE
  • google.maps.Animation.CUSTOM_FADE ( 可能吗?

我的谷歌地图V3工作代码

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>
  <script type="text/javascript">
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var infowindow = new google.maps.InfoWindow();
    var marker, i;
    function marker(i) {
        if (i > locations.length) return;
        var marker;
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            animation: google.maps.Animation.DROP,
            map: map
            });
        var t=setTimeout("marker("+(i+1)+")",500);
    }
    marker(0);
  </script>
</body>
</html>
这是我走了

多远。Google Maps API 在页面上添加了 CSS 关键帧代码段,内容 http://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/14/4/%7Bcommon,map,util,marker%7D.js

在页面内,它看起来像这样

@-webkit-keyframes _gm2657 {
0 % {
    -webkit-transform: translate3d(0px, 0px, 0); -webkit-animation-timing-function: ease-out;
}
50 % {
    -webkit-transform: translate3d(0px, -20px, 0); -webkit-animation-timing-function: ease-in ;
}
100 % {
    -webkit-transform: translate3d(0px, 0px, 0); -webkit-animation-timing-function: ease-out;
}

}

其中的函数被压缩为

function uJ(a, b) {
var c = [];
c[A]("@-webkit-keyframes ", b, " {''n");
K(a.b, function (a) {
    c[A](100 * a[$k], "% { ");
    c[A]("-webkit-transform: translate3d(", a[Fv][0], "px,", a[Fv][1], "px,0); ");
    c[A]("-webkit-animation-timing-function: ", a.wa, "; ");
    c[A]("}''n")
});
c[A]("}''n");
return c[Mc]("")
}

我现在无法再深入研究它来弄清楚如何入侵该对象。

问题是我不知道他们是如何构建关键帧动画名称的。我已经从上面的文件中得到了a.d="_gm"+l[B](1E4*l[Qb]()),但祝你好运,跟踪它回到 l[B] 是什么或对象 1E4*l 等于什么,或者 Qb 是如何定义的。

我的下一个方法是抓取包含@-webkit-keyframes的脚本标签的HTML_gm,看看提取使用的数字,然后用它来编写你自己的关键帧动画,并希望谷歌地图使用你注入的CSS而不是它自己的。

按照这些思路,我写了一个小的jQuery方法来搜索所有带有部分匹配"_gm"的样式属性的div。

var poll;
var ntrvl = setInterval(function () {
    poll = $("div[style*=' _gm']");
    if (poll.length > 0) {
        craftNewAnimation(poll.css('-webkit-animation-name'));
        clearInterval(ntrvl);
    }
}, 10);
function craftNewAnimation(name) {
    console.log(name);
    var markup = ['<style>',
        '@-webkit-keyframes ' + name + ' {',
        '0%{ opacity: 0; }',
        '100%{ opacity: 1; }',
        '}',
        '</style'
    ];
    $('body').append(markup.join(""));
}

http://jsbin.com/uJAdaJA/3/edit

由于某种原因,它不会替换它检测到的第一个动画。可能还有其他方法可以在没有太多黑客攻击的情况下获得这个神奇的关键帧名称。

祝你好运。

    var locations = [
      ['Surat', 21.205386, 72.847301, 4],
      ['Mumbai', 19.068789, 72.870265, 5]
    ];
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 7,
      center: new google.maps.LatLng(20.120783, 71.8517917),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    });
    var infowindow = new google.maps.InfoWindow();
    var marker, i;
    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        draggable:true,
        animation: google.maps.Animation.DROP,
        animation: google.maps.Animation.BOUNCE,    
      });
      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
        var bounds = new google.maps.LatLngBounds();
// in the marker-drawing loop:
bounds.extend(pos);
// after the loop:
map.fitBounds(bounds);

您可以使用Google的丰富标记实用程序,该实用程序允许您使用html元素作为标记。 现在您可以根据需要为元素提供CSS动画,例如使用丹尼登的动画.css。

举个例子:

    var pictureLabel = document.createElement("img");
    pictureLabel.src = YOUR_SOURCE;
    pictureLabel.width = SOME_WIDTH;
    pictureLabel.height = SOME_HEIGHT;
    // daneden even got the exact animation your'e looking for :)
    pictureLabel.className = pictureLabel.className + " animated fadeInDown";
    var marker = new RichMarker({
    position: new google.maps.LatLng(YOUR_LAT, YOUR_LNG),
        map: YOUR_MAP,
        content: pictureLabel
    });

如果您使用Google的丰富标记,请不要忘记使用它的点击错误修复!