谷歌地图动画热图

时间:2011-08-02 21:18:58

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

我想知道是否可以设置我为Google地图设置的热图的动画。设置谷歌地图并添加热图很容易。但它看起来很无聊。我想在热图中添加一些脉冲效果。 感谢

2 个答案:

答案 0 :(得分:4)

为热图的外观设置动画很简单。以下示例设置渐变颜色的自定义数组并调整其大小。结果是一种脉冲效应。您可以在modulateGradient函数中根据自己的喜好调整颜色,甚至可以添加对热图不透明度的更改。但请注意,根据数据集的大小,动画可能会对性能产生相当大的影响。

HTML

<div id="map-canvas"></div>

JS

var map, pointarray, heatmap;
var gradient, gradientStep = -1;

var taxiData = [
    new google.maps.LatLng(37.782551, -122.445368),
    new google.maps.LatLng(37.782745, -122.444586),
    new google.maps.LatLng(37.782842, -122.443688),
    new google.maps.LatLng(37.782919, -122.442815),
    new google.maps.LatLng(37.782992, -122.442112),
    new google.maps.LatLng(37.783100, -122.441461),
    new google.maps.LatLng(37.783206, -122.440829),
    new google.maps.LatLng(37.783273, -122.440324),
    new google.maps.LatLng(37.783316, -122.440023),
    new google.maps.LatLng(37.783357, -122.439794),
    new google.maps.LatLng(37.783371, -122.439687)
];

function initialize() {
    var mapOptions = {
        zoom: 13,
        center: new google.maps.LatLng(37.774546, -122.433523),
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var pointArray = new google.maps.MVCArray(taxiData);

    heatmap = new google.maps.visualization.HeatmapLayer({
        data: pointArray
    });

    heatmap.setMap(map);
    setGradient();

    google.maps.event.addListenerOnce(map, 'tilesloaded', modulateGradient);
}

function setGradient() {
    gradient = [
        'rgba(0, 255, 255, 0)',
        'rgba(0, 255, 255, 1)',
        'rgba(0, 191, 255, 1)',
        'rgba(0, 127, 255, 1)',
        'rgba(0, 63, 255, 1)',
        'rgba(0, 0, 255, 1)',
        'rgba(0, 0, 223, 1)',
        'rgba(0, 0, 191, 1)',
        'rgba(0, 0, 159, 1)',
        'rgba(0, 0, 127, 1)',
        'rgba(63, 0, 91, 1)',
        'rgba(127, 0, 63, 1)',
        'rgba(191, 0, 31, 1)',
        'rgba(255, 0, 0, 1)'
    ];
    heatmap.set('gradient', gradient);
}

function modulateGradient() {
    var modulator = function() {
        var newGradient = gradient.slice(0, heatmap.get('gradient').length + gradientStep);

        if (newGradient.length == gradient.length || newGradient.length == 7) {
            gradientStep *= -1;
        }

        heatmap.set('gradient', newGradient);

        setTimeout(modulator, 100);
    };

    setTimeout(modulator, 100);
}

google.maps.event.addDomListener(window, 'load', initialize);

您可以在JSFiddle上找到该代码的实时版本。

答案 1 :(得分:0)

您可以通过更改热图图层的半径大小来创建一种脉冲效果。

首先让var smallRadiusvar bigRadius为其设置值;例如smallRadius = 40; bigRadius = 80;

然后将半径大小添加到var mapOptions并将其设置为smallRadius

然后创建一个animateRadius()函数,如果将半径值设置为smallRadius,则将半径值从bigRadius更改为smallRadius,否则将半径设置为smallRadius

然后通过将函数包围在setInterval()

中,将函数设置为每秒左右运行一次

基本上就是这样:

var smallRadius = 40;
var bigRadius = 80;

    var mapOptions = {
            zoom: 13,
            center: new google.maps.LatLng(37.774546, -122.433523),
            mapTypeId: google.maps.MapTypeId.SATELLITE,
            radius: smallRadius
        };

//animate radius function

setInterval(function animateRadius()
         {
             if (heatMap.get('radius') === smallRadius)
            {
                heatMap.set('radius', bigRadius);
            }
            else
            {
                heatMap.set('radius', smallRadius);
            }
         }, 1000); // changes radius every 1 second

显然,您可以将smallRadiusbigRadius更改为您想要的任何值。

相关问题