【问题标题】:Google Maps Javascript - Make Marker BounceGoogle Maps Javascript - 使标记反弹
【发布时间】:2017-08-01 19:13:30
【问题描述】:

我一直在玩 Google Maps javascript,希望有人可以帮助我让我的标记弹跳。我有一组标记,我希望链接、div 或标题(h1)的鼠标悬停可以使特定标记反弹。

function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 11,
        center: {
            lat: -37.800426,
            lng: 145.038494
        }
    });
    setMarkers(map);
}

// Data for the markers consisting of a name, a LatLng and a zIndex for the
// order in which these markers should display on top of each other.

var stores = [
    ['A', -37.771624, 144.888128, 1],
    ['B', 37.843956, -144.994875, 2],
    ['C', -37.818086, 144.995699, 3],
    ['D', 37.812697, -145.229200, 4],
];

function setMarkers(map) {
    // Adds markers to the map.
    for (var i = 0; i < stores.length; ++i) {
        var store = stores[i];
        var marker = new google.maps.Marker({
            position: {
                lat: store[1],
                lng: store[2]
            },
            map: map,
            animation: google.maps.Animation.DROP,
            title: store[0],
            zIndex: store[3],
        });
        attachStoreTitle(marker);
    }
}

// Attaches an info window to a marker with the provided message. When the
// marker is clicked, the info window will open with the secret message.
function attachStoreTitle(marker, storeName) {
    var infowindow = new google.maps.InfoWindow({
        content: marker.title
    });

    marker.addListener('click',
        function() {
            infowindow.open(marker.get('map'), marker);
        });
}

考虑到所有数据都在一个数组中,我遇到的另一个主要问题是调用正确的标记。

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 maps


    【解决方案1】:

    此处记录了标记动画:Google Maps Marker Animations

    您可以添加以下方法:

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

    现在为你的 for 循环中的每个标记添加一个点击监听器:

    marker.addListener('click', toggleBounce);

    如果您想在悬停时调用弹跳,请改为添加 mouseover 侦听器。

    其他活动可以找here

    【讨论】:

    • 我已将 toggleBounce 函数添加到脚本底部。但是我有点困惑,因为标记在数组中。如何让每个标记单独弹跳?
    • 如果您查看顶部脚本,您将看到不同标记的数据数组。当我单击它们或鼠标悬停时,如何让每个标记反弹。
    • @Matthew 就像你对attachStoreTitle(marker) 所做的一样。之后运行marker.addListener('mouseover', toggleBounce);
    • 所以现在我的代码如下,'var marker = new google.maps.Marker({ position: {lat: store[1], lng: store[2]}, map: map,动画:google.maps.Animation.DROP,标题:商店[0],zIndex:商店[3],}); attachStoreTitle(标记);切换弹跳(标记); }' 我还添加了 'marker.addListener('mouseover', toggleBounce);'
    • 'function toggleBounce(marker) { if (marker.getAnimation() !== null) { marker.setAnimation(null); } else { marker.setAnimation(google.maps.Animation.BOUNCE); } marker.addListener('mouseover', toggleBounce); }'
    猜你喜欢
    • 2010-12-23
    • 2011-12-11
    • 1970-01-01
    • 2022-11-26
    • 2012-05-24
    • 2015-07-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多