








近期有个用户希望在之前的电子地图上增加热力图,查阅了天地图的官方接口示例,在开源库对应页面,有对应的示例完整源码,核心就是使用的开源的HeatmapOverlay.js,作为覆盖物叠加到瓦片图层上,和之前的圆形矩形同一个类别的东西。按照提供的示例,在之前的代码中增加一个addHeatMap的函数,参数传入一堆数据,这些数据包括名称、经纬度、值,为了方便解析,多个数据通过英文竖杠隔开,在js中分割开来生成数组再传给headmapoverlay对象即可。
一般一个瓦片地图上就一个热力图,后面可以通过更新数据来改变,而不是添加多个热力图,因为热力图在缩放合并的时候,需要根据数据重新计算热力区域并绘制,如果是分开的热力图对象,则并不会合并计算。热力图(Heatmap)是一种通过颜色变化来可视化数据密度或强度的图形表示方法,常用于展示二维空间中点的分布密集程度。在地理信息系统(GIS)、网页点击分析、生物信息学、金融风控等领域广泛应用。


void MapObjTian::addOverlay()
{
//http://lbs.tianditu.gov.cn/api/js4.0/class.html
//添加符号标绘/其他的可以自行增加/具体函数接口在文档的左下角符号标绘大类中
//一开始找遍文档没有发现如何设置填充的属性/尽管设置了填充颜色/后面从源码中才发现还有个fill属性控制是否需要填充
html << QString(" function addCover(type, flag, points, color, weight, opacity, style, fillColor, fillOpacity) {");
html << QString(" var pts = getPoints(points);");
html << QString(" var property = getOverlayProperty(color, weight, opacity, style, fillColor, fillOpacity);");
html << QString(" var cover;");
html << QString(" if (type == 'PolylineArrow') {");
html << QString(" cover = new T.PolylineArrow(pts, property);");
html << QString(" } else if (type == 'StraightArrow') {");
html << QString(" cover = new T.StraightArrow(pts, property);");
html << QString(" } else if (type == 'DiagonalArrow') {");
html << QString(" cover = new T.DiagonalArrow(pts, property);");
html << QString(" } else if (type == 'Sector') {");
html << QString(" cover = new T.Sector(pts, property);");
html << QString(" } else if (type == 'Arc') {");
html << QString(" cover = new T.Arc(pts, property);");
html << QString(" }");
html << QString(" cover.flag = flag;");
html << QString(" map.addOverLay(cover);");
html << QString(" }");
//动态添加弧线
html << QString(" function addCurveLine(flag, points, color, weight, opacity, style) {");
html << QString(" addCover('Arc', flag, points, color, weight, opacity, style, null, 0);");
html << QString(" }");
//动态添加扇形
html << QString(" function addSector(flag, points, color, weight, opacity, style, fillColor, fillOpacity) {");
html << QString(" addCover('Sector', flag, points, color, weight, opacity, style, fillColor, fillOpacity);");
html << QString(" }");
//动态添加热力图/http://lbs.tianditu.gov.cn/api/js4.0/opensource/class/HeatmapOverlay.html
html << QString(" var heatmapOverlay;");
html << QString(" function addHeatMap(datas, radius) {");
html << QString(" var points = []");
html << QString(" var list = datas.split('|');");
html << QString(" for (var i = 0; i < list.length; ++i) {");
html << QString(" var info = list[i].split(',');");
html << QString(" points.push({name:info[0], lng:info[1], lat:info[2], count:info[3]});");
html << QString(" }");
html << QString(" heatmapOverlay = new T.HeatmapOverlay({'radius':radius});");
html << QString(" map.addOverLay(heatmapOverlay);");
html << QString(" heatmapOverlay.setDataSet({data:points, max:300});");
html << QString(" }");
//显示隐藏热力图
html << QString(" function showHeatMap() {");
html << QString(" heatmapOverlay.show();");
html << QString(" }");
html << QString(" function hideHeatMap() {");
html << QString(" heatmapOverlay.hide();");
html << QString(" }");
}
void frmMapDraw::on_btnShowHeatMap_clicked()
{
//没有添加先添加热力图
if (!isAdd) {
isAdd = true;
QStringList list;
list << "上海,121.464160,31.241570,295";
list << "闵行,121.372830,31.113500,196";
list << "普陀,121.394120,31.252730,120";
list << "徐汇,121.431200,31.190480,230";
list << "浦东,121.540370,31.224550,168";
this->runJs(QString("addHeatMap('%1', 70)").arg(list.join("|")));
} else {
this->runJs("showHeatMap()");
}
}
void frmMapDraw::on_btnHideHeatMap_clicked()
{
this->runJs("hideHeatMap()");
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。