How to dynamically adjust zoom in Heatmap.js in OpenLayers How to dynamically adjust zoom in Heatmap.js in OpenLayers json json

How to dynamically adjust zoom in Heatmap.js in OpenLayers


Figured it out... this isn't advertised in the documentation or anywhere on the internet that I could find.. however, here's how you control the radius in OpenLayers for those of you who need it. Let's keep it simple..

// create our heatmap layervar 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]);

Now add the data:

heatmap.setDataSet(data);

Here's the key. You can do pretty much anything with this list of commands:

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

So, for example, create a zoom event so the radius changes based on your radius function. For me, it was as simple as scaling the radius based on the ratio screen width(pixels)/screen width(meters). So now in your click event:

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

I hope this helps someone 'cause it drove me crazy!