javascript - Heatmap.js 未渲染

标签 javascript jquery google-maps heatmap

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

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 中渲染了 google map ,但热图本身并未在 testData 提供的经纬度数据点上渲染。

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

有人看到我在这里做了一些愚蠢的事情吗?

最佳答案

更新

作为 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 中的数据点以获得在 map 上绘制的结果:

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)会增加渲染点的颜色强度(从而使其可见!)。

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

heatmap.setDataSet(testData);

(取消注释“debugger;”在 fiddle 中调试网络控制台中的代码)

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});
}

示例中的数据点似乎都不包含在渲染 map 区域的当前边界内。因此,“!currentBounds.contains(latlng)”始终为 true,导致不会将任何点添加到 map 数据对象。

这个假设似乎是正确的,因为数据集中的第一个点(即 lat: 33.5363,lng:-117.044)实际上位于圣地亚哥(使用它来确认 http://itouchmap.com/latlong.html ),这在渲染中不可见 map 。

关于javascript - Heatmap.js 未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384017/

相关文章:

javascript - Lodash 的 debounce 功能计时器无法正常工作

javascript - 使用 Spring Boot 构建短信和视频通话应用程序

javascript - jQuery 如果不起作用?

jquery - 如何使用 jquery 调整文本框大小并获取 div 元素内的尺寸

javascript - 如何将常规 JavaScript 合并到 ReactJS 应用程序中?

javascript - 如何循环访问 DIV

javascript - 使用 javaScript 将字符串输出到 HTML

javascript - 将 google map API 标记限制为每个标记只能点击一次

html - 谷歌地图弄乱了我的页面 :)

java - 我如何监听 Google MapView 中覆盖项目的 LongClick?