Skip to content

ol-heatmap-layer

ol-heatmap-layer can render vector data as a heatmap from various backend services. It should be used with together with ol-source-vector component.

Demo

20
20

    Setup

    Plugin usage

    This component is part of the Layers plugin. If not installed globally, you need to import and use the plugin in your main.ts or use the explicit component import (see section below).

    Import and use the Layers plugin
    ts
    import { createApp } from "vue";
    import App from "./App.vue";
    
    import {
      Map,
      Layers, 
      Sources,
    } from "vue3-openlayers";
    
    const app = createApp(App);
    // ...
    app.use(Layers); 
    // ...

    Explicit import

    If you don't want to install a plugin, you can import the component explicitly. It's available as a child of the named export Layers.

    NOTE

    The following documentation refers to the plugin usage. Please make sure to adopt the component names, when you decided to use explicit component imports (e. g. <ol-map> becomes <Map.OlMap> etc.).

    Usage

    Plugin UsageExplicit Import
    <ol-heatmap-layer><Layers.OlHeatmapLayer>

    Example below shows how you can use ol-heatmap-layer and ol-source-vector to render a heatmap from a backend source.

    vue
    <template>
      <form>
        <fieldset>
          <label for="blur">Blur</label>
          <input
            type="range"
            id="blur"
            min="0"
            max="100"
            step="1"
            v-model.number="blur"
          />
          <span class="description">{{ blur }}</span>
        </fieldset>
        <fieldset>
          <label for="radius">Radius</label>
          <input
            type="range"
            id="radius"
            min="0"
            max="100"
            step="1"
            v-model.number="radius"
          />
          <span class="description">{{ radius }}</span>
        </fieldset>
      </form>
    
      <ol-map
        ref="map"
        :loadTilesWhileAnimating="true"
        :loadTilesWhileInteracting="true"
        style="height: 400px"
      >
        <ol-view
          ref="view"
          :center="center"
          :zoom="zoom"
          :projection="projection"
        />
    
        <ol-tile-layer>
          <ol-source-osm />
        </ol-tile-layer>
    
        <ol-heatmap-layer
          title="heatmap"
          :blur="blur"
          :radius="radius"
          :weight="heatmapWeight"
          :zIndex="1"
        >
          <ol-source-vector
            ref="earthquakes"
            url="https://raw.githubusercontent.com/openlayers/openlayers/main/examples/data/kml/2012_Earthquakes_Mag5.kml"
            :format="kml"
          >
          </ol-source-vector>
        </ol-heatmap-layer>
      </ol-map>
    </template>
    
    <script setup lang="ts">
    import { ref, inject } from "vue";
    
    const center = ref([101.97, 4.21]);
    const projection = ref("EPSG:4326");
    const zoom = ref(5);
    const blur = ref(20);
    const radius = ref(20);
    const format = inject("ol-format");
    const kml = new format.KML({ extractStyles: false });
    const heatmapWeight = function (feature) {
      // 2012_Earthquakes_Mag5.kml stores the magnitude of each earthquake in a
      // standards-violating <magnitude> tag in each Placemark.  We extract it from
      // the Placemark's name instead.
      const name = feature.get("name");
      const magnitude = parseFloat(name.substr(2));
      return magnitude - 5;
    };
    </script>

    Properties

    weight

    • Type: String | Function
    • Default: 'weight'

    The feature attribute to use for the weight or a function that returns a weight from a feature. Weight values should range from 0 to 1 (and values outside will be clamped to that range).

    extent

    • Type: Array

    The bounding extent for layer rendering. The layer will not be rendered outside of this extent.

    blur

    • Type: Number
    • Default: 15

    The blur size in pixels.

    radius

    • Type: Number

    The radius size in pixels.