zl程序教程

您现在的位置是:首页 >  工具

当前栏目

openlayers4 入门开发系列之热力图篇(附源码下载)

源码下载入门开发 系列 力图
2023-09-11 14:19:51 时间

前言

openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子,这个也是学习 openlayers4 的好素材。

openlayers4 入门开发系列的地图服务基于 Geoserver 发布的,关于 Geoserver 方面操作的博客,可以参考以下几篇文章:

内容概览

1.基于 openlayers4 实现地图热力图
2.源代码 demo 下载

本篇的重点内容是利用 openlayers4 实现热力图功能,效果图如下:

实现思路

  • 热力图界面设计
//热力图
"<div style='height:25px;background:#30A4D5;margin-top:2px;width: 98%;margin-left: 3px;'>" +
"<span style='margin-left:5px;font-size: 13px;color:white;'>热力图</span>" +
"</div>" +
'<div id="heatmapFeatureLayer" style="padding:5px;">' +
'<div style="float:left;">' +
'<input type="checkbox" name="heatmapFeatureLayer" id="heatmap1" style="width: 15px;height: 15px;vertical-align: middle;margin: auto;"/>' +
'<label style="font-weight: normal;vertical-align: middle;margin: auto;">热力图</label>' +
'</div>' +
'</div>' +
  • 热力图点击事件
//加载热力图
$("#heatmapFeatureLayer input").bind("click", function () {
if (this.checked) {
if(!bxmap.olHeatMap.isLoad){
bxmap.olHeatMap.Init(bmap);
} else{
bxmap.olHeatMap.showHeatMapLayer();
}
//图例面板显示
$("#map_tl").css("display","block");
$("#map_tl>img").attr('src', getRootPath() +"js/main/olHeatMap/lend_dz.png");
$("#map_tl>img").css("width","165px");
$("#map_tl>img").css("height","165px");
}
else {
bxmap.olHeatMap.hideHeatMapLayer();
//图例面板隐藏
$("#map_tl").hide();
}
})
热力图初始化以及核心代码实现
Init:function(bmap){
//加载热力图
this.map = bmap.getMap();
this.isLoad = true;
this.initHeatMapLayer(dz);
},
/**
* 初始化加载-热力图
*/
initHeatMapLayer:function(data){
var num = data.features.length;
if (num > 0) {
var features = new Array(num);
for (var i = 0; i < num; i++) {
var geo = data.features[i].geometry;
var coordinate = [geo.x, geo.y];
features[i] = new ol.Feature({
geometry: new ol.geom.Point(coordinate),
weight: data.features[i].attributes[field_dz]
});
}
this.loadHeatLayer(features);
}
},
loadHeatLayer:function(features){
var self = bxmap.olHeatMap;
self.layer = self.createHeatMap({ features: features, radius: self.radius, gradient: self.gradient1 });//创建热力图层
self.map.addLayer(self.layer);
self.map.getView().on('change:resolution', self.handleHeatMap);
//缩放至范围
self.map.getView().fit(self.layer.getSource().getExtent(), self.map.getSize());
 
},

更多的详情见GIS之家小专栏

文章尾部提供源代码下载,对本专栏感兴趣的话,可以关注一波