三十七、openlayers官网示例Earthquakes Heatmap解析——在地图上加载热力图

官网demo地址:

Earthquakes Heatmap 

这篇主要介绍了热力图HeatmapLayer

HeatmapLayer 是一个用于在地图上显示热力图的图层类型,通常用于表示地理数据中的密度或强度。例如,它可以用来显示地震、人口密度或其他空间数据的热点区域。在这个示例中,HeatmapLayer 被用来显示从 KML 文件中提取的地震数据。 

 const vector = new HeatmapLayer({
      source: new VectorSource({
        url: "https://openlayers.org/en/latest/examples/data/kml/2012_Earthquakes_Mag5.kml",
        format: new KML({
          extractStyles: false,
        }),
      }),
      //热力图的模糊半径,以像素为单位。
      blur: parseInt(blur.value, 10),
      //每个点的影响半径,以像素为单位。
      radius: parseInt(radius.value, 10),
      //用于根据特征属性计算热力图中每个点的权重 权重值 应介于 0 到 1 之间
      weight: function (feature) {
        const name = feature.get("name");
        const magnitude = parseFloat(name.substr(2));
        console.log("magnitude", magnitude);
        return magnitude - 5;
      },
    });

通过滑块的改变控制图层的半径和模糊度 

 blur.addEventListener("input", function () {
      vector.setBlur(parseInt(blur.value, 10));
    });

    radius.addEventListener("input", function () {
      vector.setRadius(parseInt(radius.value, 10));
    });

 完整代码:

<template>
  <div class="box">
    <h1>Earthquakes Heatmap</h1>
    <div id="map"></div>
    <form>
      <label for="radius">radius size</label>
      <input id="radius" type="range" min="1" max="50" step="1" value="5" />
      <label for="blur">blur size</label>
      <input id="blur" type="range" min="1" max="50" step="1" value="15" />
    </form>
  </div>
</template>

<script>
import KML from "ol/format/KML.js";
import Map from "ol/Map.js";
import StadiaMaps from "ol/source/StadiaMaps.js";
import VectorSource from "ol/source/Vector.js";
import View from "ol/View.js";
import { Heatmap as HeatmapLayer, Tile as TileLayer } from "ol/layer.js";
export default {
  name: "",
  components: {},
  data() {
    return {
      map: null,
    };
  },
  computed: {},
  created() {},
  mounted() {
    const blur = document.getElementById("blur");
    const radius = document.getElementById("radius");

    const vector = new HeatmapLayer({
      source: new VectorSource({
        url: "https://openlayers.org/en/latest/examples/data/kml/2012_Earthquakes_Mag5.kml",
        format: new KML({
          extractStyles: false,
        }),
      }),
      //热力图的模糊半径,以像素为单位。
      blur: parseInt(blur.value, 10),
      //每个点的影响半径,以像素为单位。
      radius: parseInt(radius.value, 10),
      //用于根据特征属性计算热力图中每个点的权重 权重值 应介于 0 到 1 之间
      weight: function (feature) {
        const name = feature.get("name");
        const magnitude = parseFloat(name.substr(2));
        console.log("magnitude", magnitude);
        return magnitude-5;
      },
    });

    const raster = new TileLayer({
      source: new StadiaMaps({
        layer: "stamen_toner",
      }),
    });

    new Map({
      layers: [raster, vector],
      target: "map",
      view: new View({
        center: [0, 0],
        zoom: 2,
      }),
    });

    blur.addEventListener("input", function () {
      vector.setBlur(parseInt(blur.value, 10));
    });

    radius.addEventListener("input", function () {
      vector.setRadius(parseInt(radius.value, 10));
    });
  },
  methods: {},
};
</script>

<style lang="scss" scoped>
#map {
  width: 100%;
  height: 500px;
}
.box {
  height: 100%;
}
</style>

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 04:06:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 04:06:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 04:06:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 04:06:03       20 阅读

热门阅读

  1. conda环境里安装ffmpeg

    2024-06-06 04:06:03       9 阅读
  2. 在本地局域网的 Ubuntu 16.04 主机安装 GitLab 服务

    2024-06-06 04:06:03       11 阅读
  3. 正则表达式 0.1v

    2024-06-06 04:06:03       9 阅读
  4. WHAT - 容器化系列(二)- docker

    2024-06-06 04:06:03       11 阅读
  5. Shell正则表达式与文本处理器

    2024-06-06 04:06:03       6 阅读
  6. 从0开发一个Chrome插件:创建第一个Chrome插件

    2024-06-06 04:06:03       11 阅读
  7. SASS语法基础

    2024-06-06 04:06:03       8 阅读
  8. CentOS 7 64位 常用命令

    2024-06-06 04:06:03       6 阅读
  9. 03-3.1.1 栈的基本概念

    2024-06-06 04:06:03       9 阅读
  10. 日常工作笔记

    2024-06-06 04:06:03       10 阅读