gpt4 book ai didi

javascript - 如何在 OpenLayers 中动态调整 Heatmap.js 中的缩放

转载 作者:行者123 更新时间:2023-11-29 14:54:17 24 4
gpt4 key购买 nike

考虑以下代码:

heatmap = new OpenLayers.Layer.Heatmap( "Heatmap Layer", map, osm,
{visible: true, radius: radiusForScale[zoom], legend: {position: 'br',title: 'title'}},
{isBaseLayer: false, opacity: 0.3, projection: new OpenLayers.Projection("EPSG:4326")}
);

背景:heatmap.js 非常适合显示人口密度等...但是,我不希望 heatmap.js 控制我的点的半径。相反,我希望根据我创建的自定义缩放功能动态更新半径,而不必破坏热图并在每次缩放更改时重新创建它。现在,如果您按如下方式将热图添加到 div,这是可能的:

var config = {
element: document.getElementById("heatmapArea"),
radius: 30,
opacity: 50
};

//creates and initializes the heatmap
var heatmap = h337.create(config);

在此示例中,它就像更新 JSON 对象和更新显示一样简单。但是,这只是分配给 div 的静态显示,因此使矢量图层无用。有没有人在 OpenLayers 中对此有任何成功??

旁注:我浏览了热图 JSON 字符串中的所有键,但似乎没有办法更改缩放变量。

最佳答案

想通了...这并没有在文档中或我能在互联网上找到的任何地方做广告...但是,对于那些需要它的人,这里是您如何在 OpenLayers 中控制半径。让我们保持简单..

// create our heatmap layer
var heatmap = new OpenLayers.Layer.Heatmap(
"Heatmap Layer", map, osm, {visible: true, radius:50},
{isBaseLayer: false, opacity: 0.3, projection: new OpenLayers.Projection("EPSG:4326")}
);
map.addLayers([heatmap]);

现在添加数据:

heatmap.setDataSet(data);

这是关键。您几乎可以使用此命令列表执行任何操作:

this.set("radius",value); //****this one solved my problem
this.set("element",value);
this.set("visible",value); //deceiving! You have to access the canvas subclass to get it to work
this.set("max",value);
this.set("gradient",value);
this.set("opacity",value);
this.set("width",value);
this.set("height",value);
this.set("debug",value);

因此,例如,创建一个缩放事件,以便半径根据您的半径函数发生变化。对我来说,这就像根据屏幕宽度(像素)/屏幕宽度(米)的比例缩放半径一样简单。所以现在在你的点击事件中:

on zoom change: //pseudocode
canvas = heatmap.heatmap.get('canvas'); //this is the subclass I spoke of above
if the zoom is above a certain value //pseudocode
then
canvas.style.display = 'block'; //show the heatmap... you can also use heatmap.toggle() but this gives you more control
heatmap.heatmap.set('radius',zoomfunction[map.zoom]); //this dynamically updates the radius!!
heatmap.updateLayer();
else
canvas.style.display = 'none';
heatmap.updateLayer();

我希望这对某人有所帮助,因为它让我发疯了!

关于javascript - 如何在 OpenLayers 中动态调整 Heatmap.js 中的缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20548700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com