分析 Earthquakes Heatmap 这个 demo,官网介绍是:
This example parses a KML file and renders the features as a ol/layer/Heatmap
layer.
这个示例解析了 KML 格式文件并作为一个热力图来渲染要素。
定义基本结构
这个示例比较简单,地图的基本结构如下所示,raster
图层使用的是普通的 toner
碳粉地图, vector
矢量图层的定义见下节。在 DOM 中,添加两个 range
类型的 input
标签,用于控制热力图的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css" /> <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script> <title>Earthquakes Heatmap(地震热力图)</title> <style> html, body, .map { height: 100%; width: 100%; } </style> </head>
<body> <form> <label>半径大小</label> <input id="radius" type="range" min="1" max="50" step="1" value="5" /> <label>模糊大小</label> <input id="blur" type="range" min="1" max="50" step="1" value="15" /> </form> <div id="map" class="map"></div>
<script type="text/javascript"> let raster = new ol.layer.Tile({ source: new ol.source.Stamen({ layer: "toner" }) });
let map = new ol.Map({ layers: [raster, vector], target: "map", view: new ol.View({ center: [0, 0], zoom: 2 }) }); </script> </body> </html>
|
Heatmap 矢量图层
关于 ol.layer.Heatmap
对象的API如下图所示:
除了 source
属性外,设置热力图最关键的几个属性是 blur
(热力圆圈的模糊大小)、radius
(热力圆圈的半径),还有一个 weight
权重属性。weight
属性是一个回调函数,入参是 feature
要素,返回的结果应该是一个范围为[0, 1]
的的权重值,权重越大就越热。
blur
和 radius
取得是两个 input
标签中的值。source
属性中的ol.layer.Vector读取的是 KML 格式的 2012 年世界 5 级地震数据,所以所有的震级都是五点几,减去 5
后,自然会返回一个[0, 1]
的的权重值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| let blur = document.getElementById("blur"); let radius = document.getElementById("radius"); let vector = new ol.layer.Heatmap({ source: new ol.source.Vector({ url: "https://openlayers.org/en/latest/examples/data/kml/2012_Earthquakes_Mag5.kml", format: new ol.format.KML({ extractStyles: false }) }), blur: parseInt(blur.value, 10), radius: parseInt(radius.value, 10), weight: feature => { let name = feature.get("name"); let magnitude = parseFloat(name.substr(2)); return magnitude - 5; } });
|
事件绑定
这里就是普通的事件回调,给两个 input
标签设置两个 input
和 change
事件绑定,触发时就会调用setBlur和setRadius方法给 Heatmap
设置 blur
和 radius
值,并同步到地图中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let blurHandler = () => { vector.setBlur(parseInt(blur.value, 10)); };
blur.addEventListener("input", blurHandler); blur.addEventListener("change", blurHandler);
let radiusHandler = () => { vector.setRadius(parseInt(radius.value, 10)); };
radius.addEventListener("input", radiusHandler); radius.addEventListener("change", radiusHandler);
|
全部代码
全部代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css" /> <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script> <title>Earthquakes Heatmap(地震热力图)</title> <style> html, body, .map { height: 100%; width: 100%; } </style> </head>
<body> <form> <label>半径大小</label> <input id="radius" type="range" min="1" max="50" step="1" value="5" /> <label>模糊大小</label> <input id="blur" type="range" min="1" max="50" step="1" value="15" /> </form> <div id="map" class="map"></div>
<script type="text/javascript"> let blur = document.getElementById("blur"); let radius = document.getElementById("radius"); let vector = new ol.layer.Heatmap({ source: new ol.source.Vector({ url: "https://openlayers.org/en/latest/examples/data/kml/2012_Earthquakes_Mag5.kml", format: new ol.format.KML({ extractStyles: false }) }), blur: parseInt(blur.value, 10), radius: parseInt(radius.value, 10), weight: feature => { let name = feature.get("name"); let magnitude = parseFloat(name.substr(2)); return magnitude - 5; } });
let raster = new ol.layer.Tile({ source: new ol.source.Stamen({ layer: "toner" }) });
let map = new ol.Map({ layers: [raster, vector], target: "map", view: new ol.View({ center: [0, 0], zoom: 2 }) }); let blurHandler = () => { vector.setBlur(parseInt(blur.value, 10)); }; blur.addEventListener("input", blurHandler); blur.addEventListener("change", blurHandler); let radiusHandler = () => { vector.setRadius(parseInt(radius.value, 10)); }; radius.addEventListener("input", radiusHandler); radius.addEventListener("change", radiusHandler); </script> </body> </html>
|