Heatmap.js无法渲染

时间:2013-12-04 19:20:22

标签: javascript jquery google-maps heatmap

我正在尝试将heatmap.js与Google地图集成以进行一些可视化工作。我提到了这个:

http://www.patrick-wied.at/static/heatmapjs/example-heatmap-googlemaps.html

因此,我本地样本中的代码看起来几乎相同:

<!DOCTYPE html>
<html lang="en">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">    </script>
<div id="heatmapArea" style="width:1024px;padding:0;height:768px;cursor:pointer;position:relative;"></div>
<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/heatmap.js"></script>
<script type="text/javascript" src="js/heatmap-gmaps.js"></script>
<script type="text/javascript">
    window.onload = function(){
    // standard gmaps initialization
    var myLatlng = new google.maps.LatLng(48.3333, 16.35);
    // define map properties
    var myOptions = {
      zoom: 3,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      disableDefaultUI: false,
      scrollwheel: true,
      draggable: true,
      navigationControl: true,
      mapTypeControl: false,
      scaleControl: true,
      disableDoubleClickZoom: false
    };
    // we'll use the heatmapArea 
    var map = new google.maps.Map($("#heatmapArea")[0], myOptions);

    // let's create a heatmap-overlay
    // with heatmap config properties
    var heatmap = new HeatmapOverlay(map, {
        "radius":20,
        "visible":true, 
        "opacity":60
    });

    // here is our dataset
    // important: a datapoint now contains lat, lng and count property!
    var testData={
            max: 46,
            data: [{lat: 33.5363, lng:-117.044, count: 1},{lat: 33.5608, lng:-117.24,     count: 1},{lat: 38, lng:-97, count: 1},{lat: 38.9358, lng:-77.1621, count: 1}]
    };

    // now we can set the data
    google.maps.event.addListenerOnce(map, "idle", function(){
        // this is important, because if you set the data set too early, the     latlng/pixel projection doesn't work
        heatmap.setDataSet(testData);
    });

};
</script>
</html>

我正在按照我的预期在我的div中获取谷歌地图渲染,但热图本身不会在testData提供的lat-long数据点上呈现。

浏览器控制台没有错误....

有人看到我在这里做些蠢事吗?

1 个答案:

答案 0 :(得分:13)

更新

由于heatmap.js v1现在已经retired,我还提供了一个基于当前版本的示例,v2,这里:

http://jsfiddle.net/Fresh/pz4usuLe/

这基于Google Maps example found on the heatmap website

原始答案

我看了这个,这绝对令人困惑。我拿了你的代码并让它在这里工作:

http://jsfiddle.net/Fresh/nRQbd/

(请注意,上面的原始示例不再有效,因为v1已经停用,要看到这与v2一起使用,请参阅此http://jsfiddle.net/Fresh/pz4usuLe/

请注意我如何修改testData中的数据点以获取在地图上绘制的结果:

var testData = {
    max: 3,
    data: [{
        lat: 48.3333,
        lng: 16.35,
        count: 100
    }, {
        lat: 51.465558,
        lng: 0.010986,
        count: 100
    }, {
        lat: 33.5363,
        lng: -5.044,
        count: 100
    }]
};

我还增加了每个点的计数值;点值1不可见,但增加它的值(例如增加到100)会增加渲染点颜色的强度(从而使其可见!)。

当在可见地图边界之外绘制点时,似乎会出现问题。我通过调试推断出这一点:

heatmap.setDataSet(testData);

(取消注释“调试器;”在小提琴中调试Web控制台中的代码)

heatmap-gmaps.js中导致数据点无法呈现的代码在这里:

while(dlen--){
 latlng = new google.maps.LatLng(d[dlen].lat, d[dlen].lng);
 if(!currentBounds.contains(latlng)) {
  continue;
  }
 me.latlngs.push({latlng: latlng, c: d[dlen].count});
 point = me.pixelTransform(projection.fromLatLngToDivPixel(latlng));
 mapdata.data.push({x: point.x, y: point.y, count: d[dlen].count});
}

看来,示例中的数据点都不包含在渲染地图区域的当前边界内。因此“!currentBounds.contains(latlng)”始终为true,导致没有点添加到mapdata对象。

这个假设似乎是正确的,因为数据集中的第一个点(即纬度:33.5363,经度:-117.044)其实是在圣地亚哥(使用此确认http://itouchmap.com/latlong.html),这是不可见的渲染的地图。

相关问题