百度API-------热力图

<!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=1.4"></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>
</head>
<body>
<div id="container"></div>
<div id="r-result">
<input type="button" onclick="openHeatmap();" value="显示热力图" />
<input type="button" onclick="closeHeatmap();" value="关闭热力图" />
</div>
</body>
</html>
<script type="text/javascript">
var map = new BMap.Map("container"); // 创建地图实例
var point = new BMap.Point(108.960637, 34.355135);
map.centerAndZoom(point, 6); // 初始化地图,设置中心点坐标和地图级别
map.enableScrollWheelZoom(); // 允许滚轮缩放
var points = [
{ "lng": 108.960637, "lat": 34.355135, "count": 100 },//西安
{ "lng": 107.203695, "lat": 34.416147, "count": 100 },//宝鸡
{ "lng": 109.080219, "lat": 32.768743, "count": 100 },//安康
{ "lng": 107.019722, "lat": 33.07907, "count": 100},//汉中
{ "lng": 109.889701, "lat": 33.942135, "count": 100 },//商洛
{ "lng": 109.448165, "lat": 36.61208, "count": 100 },//延安
{ "lng": 108.933041, "lat": 34.940506, "count": 100 },//铜川
{ "lng": 109.515431, "lat": 34.506629, "count": 100 },//渭南
{ "lng": 108.704512, "lat": 34.339637, "count": 100},//咸阳
{ "lng": 109.730161, "lat": 38.305673, "count": 100 },// 榆林
];
if (!isSupportCanvas()) {
alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
}
//详细的参数,可以查看heatmap.js的文档 https://github.com/pa7/heatmap.js/blob/master/README.md
//参数说明如下:
/* visible 热力图是否显示,默认为true
* opacity 热力的透明度,1-100
* radius 势力图的每个点的半径大小
* gradient {JSON} 热力图的渐变区间 . gradient如下所示
* {
.2:'rgb(0, 255, 255)',
.5:'rgb(0, 110, 255)',
.8:'rgb(100, 0, 255)'
}
其中 key 表示插值的位置, 0~1.
value 为颜色值.
*/
heatmapOverlay = new BMapLib.HeatmapOverlay({ "radius": 20 });
map.addOverlay(heatmapOverlay);
heatmapOverlay.setDataSet({ data: points, max: 100 });
//是否显示热力图
function openHeatmap() {
heatmapOverlay.show();
}
function closeHeatmap() {
heatmapOverlay.hide();
}
closeHeatmap();
function setGradient() {
/*格式如下所示:
{
0:'rgb(102, 255, 0)',
.5:'rgb(255, 170, 0)',
1:'rgb(255, 0, 0)'
}*/
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>

 

运行效果:

 

 

posted @ 2016-10-27 15:28  李刚289237642  阅读(6330)  评论(0编辑  收藏  举报