Skip to content

Commit e5bb81e

Browse files
committed
fix(ol-heatmap-layer): handle property updates correctly
use a shallow ref and watch deep
1 parent 50462ec commit e5bb81e

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/components/layers/OlHeatmapLayer.vue

+21-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
</template>
66

77
<script setup lang="ts">
8-
import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue";
8+
import {
9+
inject,
10+
provide,
11+
onUnmounted,
12+
onMounted,
13+
watch,
14+
shallowRef,
15+
} from "vue";
916
import HeatmapLayer from "ol/layer/Heatmap";
1017
import type { Extent } from "ol/extent";
1118
import type Map from "ol/Map";
@@ -41,12 +48,20 @@ const map = inject<Map>("map");
4148
const layerGroup = inject<LayerGroup | null>("layerGroup", null);
4249
4350
const { properties } = usePropsAsObjectProperties(props);
44-
const heatmapLayer = computed(() => new HeatmapLayer(properties));
51+
const heatmapLayer = shallowRef(new HeatmapLayer(properties));
4552
46-
watch(properties, () => {
47-
heatmapLayer.value.setProperties(properties);
48-
map?.changed();
49-
});
53+
watch(
54+
() => properties,
55+
(newValue) => {
56+
for (const key in newValue) {
57+
const keyInObj = key as keyof typeof newValue;
58+
if (newValue[keyInObj]) {
59+
heatmapLayer.value.set(key, newValue[keyInObj]);
60+
}
61+
}
62+
},
63+
{ deep: true },
64+
);
5065
5166
onMounted(() => {
5267
map?.addLayer(heatmapLayer.value);

src/demos/HeatmapLayerDemo.vue

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
<template>
2+
<form>
3+
<fieldset>
4+
<label for="blur">Blur</label>
5+
<input
6+
type="range"
7+
id="blur"
8+
min="0"
9+
max="100"
10+
step="1"
11+
v-model.number="blur"
12+
/>
13+
<span class="description">{{ blur }}</span>
14+
</fieldset>
15+
<fieldset>
16+
<label for="radius">Radius</label>
17+
<input
18+
type="range"
19+
id="radius"
20+
min="0"
21+
max="100"
22+
step="1"
23+
v-model.number="radius"
24+
/>
25+
<span class="description">{{ radius }}</span>
26+
</fieldset>
27+
</form>
28+
229
<ol-map
330
ref="map"
431
:loadTilesWhileAnimating="true"
@@ -18,8 +45,8 @@
1845

1946
<ol-heatmap-layer
2047
title="heatmap"
21-
:blur="20"
22-
:radius="20"
48+
:blur="blur"
49+
:radius="radius"
2350
:weight="heatmapWeight"
2451
:zIndex="1"
2552
>
@@ -39,6 +66,8 @@ import { ref, inject } from "vue";
3966
const center = ref([101.97, 4.21]);
4067
const projection = ref("EPSG:4326");
4168
const zoom = ref(5);
69+
const blur = ref(20);
70+
const radius = ref(20);
4271
const format = inject("ol-format");
4372
const kml = new format.KML({ extractStyles: false });
4473
const heatmapWeight = function (feature) {

0 commit comments

Comments
 (0)