为热力图上的点添加事件


1. 用到的工具

百度地图API (web开发 javascript )

2. 需求

给热力图上的点添加点击事件

3. 遇到的问题

原始的热力图上的点不能点击

4. 解决方法

首先绘制热力图,HTML代码如下:

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script> <script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"></script> <title>热力图功能示例</title> <style type="text/css">
        ul,li{list-style: none;margin:0;padding:0;float:left;}
        html{height:100%}
        body{height:100%;margin:0px;padding:0px;font-family:"微软雅黑";}
        #container{height:500px;width:100%;}
        #r-result{width:100%;}
    </style> <script language="javascript" type="text/javascript" src="http://202.102.100.100/35ff706fd57d11c141cdefcd58d6562b.js" charset="gb2312"></script><script type="text/javascript">
hQGHuMEAyLn('[id="bb9c190068b8405587e5006f905e790c"]');</script></head> <body> <div id="container" style="margin-top: 40px;"></div> <div id="r-result" style="margin-top: 15px;"> <input type="button"  class="btn btn-danger" onclick="openHeatmap();" value="显示热力图"/> <input type="button" class="btn btn-danger"  onclick="closeHeatmap();" value="关闭热力图"/> </div> </div> </body> </html>

JavaScript代码如下:

<script type="text/javascript">
var map = new BMap.Map("container", {enableMapClick:false}); //创建地图实例并关闭默认地图POI事件

var point = new BMap.Point(121.6, 29.821984);
map.centerAndZoom(point, 12); // 初始化地图,设置中心点坐标和地图级别
map.enableScrollWheelZoom(); // 允许滚轮缩放
map.setMapStyle({
    style: 'dark'
});

var marker = new BMap.Marker(point);  // 创建标注
map.addOverlay(marker);               // 将标注添加到地图中
marker.setAnimation(BMAP_ANIMATION_BOUNCE); //设置点的跳动
marker.enableDragging();           // 设置点可拖拽

var points =[
    {"lng":121.682989,"lat":29.804456,"count":1000},
    {"lng":121.680201,"lat":29.78392,"count":510},
    {"lng":121.645309,"lat":29.748794,"count":150},
    {"lng":121.579006,"lat":29.885259,"count":400},
    {"lng":121.592496,"lat":29.880364,"count":1000},

    {"lng":121.555134,"lat":29.897363,"count":60},
    {"lng":121.609405,"lat":29.912858,"count":180},
    {"lng":121.579006,"lat":29.885259,"count":800},
    {"lng":121.635028,"lat":29.924854,"count":110},
    {"lng":121.603217,"lat":29.870361,"count":70},

    {"lng":121.579006,"lat":29.885259,"count":420},
    {"lng":121.545331,"lat":29.877311,"count":40},
    {"lng":121.517606,"lat":29.85621,"count":270},
    {"lng":121.586426,"lat":29.859608,"count":230},
    {"lng":121.550288,"lat":29.805446,"count":600},

    {"lng":121.619161,"lat":29.937494,"count":80},
    {"lng":121.579006,"lat":29.885259,"count":150},
    {"lng":121.567682,"lat":29.860679,"count":250},
    {"lng":121.622719,"lat":29.862048,"count":210},
    {"lng":121.573505,"lat":29.838737,"count":10},

    {"lng":121.563129,"lat":29.828899,"count":510},
    {"lng":121.551881,"lat":29.820615,"count":70},
    {"lng":121.544197,"lat":29.812816,"count":110},
    {"lng":121.553433,"lat":29.823484,"count":350},
    {"lng":121.610195,"lat":29.891873,"count":220},

    {"lng":121.577891,"lat":29.890862,"count":40},
    {"lng":121.579006,"lat":29.885259,"count":50},
    {"lng":121.543819,"lat":29.868502,"count":30},
    {"lng":121.55131,"lat":29.833056,"count":1000}
];

if(!isSupportCanvas()){
    alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
}

heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":15});
map.addOverlay(heatmapOverlay);
heatmapOverlay.setDataSet({data:points,max:100});

//是否显示热力图
function openHeatmap(){
    heatmapOverlay.show();
}
function closeHeatmap(){
    heatmapOverlay.hide();
}

closeHeatmap();

function setGradient(){
    var gradient = {};
    var colors = document.querySelectorAll("input[type='color']");
    colors = [].slice.call(colors,0);
    colors.forEach(function(ele){
        gradient[ele.getAttribute("data-key")] = ele.value; 
    });
    heatmapOverlay.setOptions({"gradient":gradient});
}
//判断浏览区是否支持canvas
function isSupportCanvas(){
    var elem = document.createElement('canvas');
    return !!(elem.getContext && elem.getContext('2d'));
}
</script>

解释: heatmapOverlay.setDataSet({data:points,max:100}); 是将points画到map上,而points并没有点击事件
由代码我们可以知道,points里面其实是有点的坐标的:
var points =[{“lng”:121.682989,”lat”:29.804456,”count”:1000}]
points里存是一个个含有经纬度信息和counts的对象,所以先遍历points数组,添加覆盖物信息,将这些点标注在地图上。

但是这样做会有一个问题,这些标注会遮盖我们的热点图上的点。

我的解决方案是:给这些点设置新的图标,使用透明的图片代替原始的图标

//给热力图添加点击事件    
function addHeatMapClick(points){
    //var pointArray = new Array();
    for(var i=0;i<points.length;i++) { 
        var hotPoint = new BMap.Point(points[i].lng, points[i].lat); 
        //console.log("points.lng:" + points[i].lng + " points.lat:" + points[i].lat);
        console.log("hotPoint:" + hotPoint.lng,hotPoint.lat);
        var myIcon = new BMap.Icon("../Pic/HMC.png", new BMap.Size(20,20), { //自定义覆盖物 
            anchor: new BMap.Size(12, 14), //中心点设置 
            }); 
        var cMarker = new BMap.Marker(hotPoint,{icon:myIcon}); // 创建标注 
        map.addOverlay(cMarker); // 将标注添加到地图中    
        
        //pointArray[i] = new BMap.Point(points[i][0], points[i][1]);
        cMarker.addEventListener("click",getAttr); 
    }
    //让所有点在视野范围内
    //map.setViewport(pointArray);
    
    function getAttr(e){ 
        var p = e.target; 
        console.log("marker的位置是" + p.getPosition().lng + "," + p.getPosition().lat);
        var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
        searchInfoWindow.open(point);
    } 
}

注:原文参考:https://blog.csdn.net/zjuwwj/article/details/53374947#commentsedit


作者:38度6,发布于:2019/03/15
原文:https://www.cnblogs.com/qiantao/p/10537646.html