动画热图层

时间:2015-02-02 12:56:21

标签: javascript google-maps heatmap

我需要稍后制作热图动画。到目前为止我有这个代码:

HTML

<div id="filteredMap" style="margin-top:15px;" class="col-md-8 col-md-offset-0 col-sm-5 col-sm-offset-3 col-xs-10 col-xs-offset-2 form-group center-block">
    <span class="label label-warning">Google map can not be loaded, or you have not filtered the full map.</span>
</div>

的JavaScript

$(document).ready(function () {
var heatMapDataFiltered = [];
var heatmapFiltered;
var mapFiltered;
var mapOptionsFiltered;

function initialize() {
    mapOptionsFiltered = {
        center: { lat: @ViewBag.lat, lng: @ViewBag.lng },
        zoom: 15,
        maxZoom: 18,
        minZoom:14
    };

    mapFiltered = new google.maps.Map(document.getElementById('filteredMap'),
    mapOptionsFiltered);

    heatmapFiltered = new google.maps.visualization.HeatmapLayer({
        data: heatMapDataFiltered
    });

    heatmapFiltered.setMap(mapFiltered);
    heatmapFiltered.set('gradient', heatmapFiltered.get('gradient') ? null : gradient);
}

$('#btnShowPulse').click(function(){
    heatmapFiltered.setMap(null);
    heatMapDataFiltered = [];
    var noDate = "no";

    if($('#NoDate').is(':checked')){
        noDate = "yes";
    }

    var venueCategoryName = $('#venueCategoryName').val();
    var venueStartingDate = $('#venueStartingDate').val();
    var venueEndingDate = $('#venueEndingDate').val();
    var URL = "/Map/FilterJSON?venueCategoryName=" + venueCategoryName + "&venueStartingDate=" + venueStartingDate + "&venueEndingDate=" + venueEndingDate + "&NoDate=" + noDate;

    $.ajax({
        type: 'get',
        dataType: 'json',
        cache: false,
        url: URL,
        success: function (data) {
            $.each(data, function (index, data) {
                heatMapDataFiltered.push({
                    location: new google.maps.LatLng(data.lat, data.lng),
                    weight: data.weight
                });

                heatmapFiltered = new google.maps.visualization.HeatmapLayer({
                    data: heatMapDataFiltered
                });

                heatmapFiltered.setMap(mapFiltered);
                heatmapFiltered.set('gradient', heatmapFiltered.get('gradient') ? null : gradient);

                setTimeout(2000);
            });
        },
        error: function (request, status, error) {
            alert(request.responseText);
        }
    });           
});

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

问题是,这段代码运行成功,但我不会在地图上逐点看到。它将显示所有的点,而不是一个接一个。我不知道为什么setTimeout无法正常工作?

1 个答案:

答案 0 :(得分:1)

setTimeout(2000)什么都不做。

setTimeout(callback, 2000)将等待两秒钟,然后执行callback功能。

这是一个带匿名函数的版本:

$.each(data, function (index, data) {
    setTimeout(function() {
        heatMapDataFiltered.push({
            location: new google.maps.LatLng(data.lat, data.lng),
            weight: data.weight
        });

        heatmapFiltered = new google.maps.visualization.HeatmapLayer({
            data: heatMapDataFiltered
        });

        heatmapFiltered.setMap(mapFiltered);
        heatmapFiltered.set('gradient', heatmapFiltered.get('gradient') ? null : gradient);

    }, 2000 * index)
});