Skip to content

Commit e9ae6fa

Browse files
committed
fix(ol-vector-layer): watch opacity / visible changes
closes #347
1 parent 2e9e2a6 commit e9ae6fa

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/components/layers/OlVectorLayer.vue

+29-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,35 @@ const properties = usePropsAsObjectProperties(props);
4343
4444
const vectorLayer = computed(() => new VectorLayer(properties));
4545
46-
watch(properties, () => {
47-
vectorLayer.value.setProperties(properties);
48-
});
46+
watch(
47+
() => properties,
48+
(newValue) => {
49+
vectorLayer.value.setProperties(newValue);
50+
for (const key in newValue) {
51+
const keyInObj = key as keyof typeof newValue;
52+
if (newValue[keyInObj]) {
53+
vectorLayer.value.set(key, newValue[keyInObj]);
54+
}
55+
}
56+
},
57+
{ deep: true },
58+
);
59+
60+
watch(
61+
() => props.visible,
62+
(newVisible: boolean) => {
63+
vectorLayer.value.setVisible(newVisible);
64+
},
65+
{ immediate: true },
66+
);
67+
68+
watch(
69+
() => props.opacity,
70+
(newOpacity: number) => {
71+
vectorLayer.value.setOpacity(newOpacity);
72+
},
73+
{ immediate: true },
74+
);
4975
5076
onMounted(() => {
5177
map?.addLayer(vectorLayer.value);

src/demos/VectorSourceDemo1.vue

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
<template>
2+
<form>
3+
<fieldset>
4+
<label for="opacity">Layer Opacity</label>
5+
<input
6+
type="range"
7+
id="opacity"
8+
min="0"
9+
max="1"
10+
step="0.1"
11+
v-model.number="opacity"
12+
/>
13+
<span class="description">{{ opacity }}</span>
14+
</fieldset>
15+
</form>
16+
217
<ol-map
318
:loadTilesWhileAnimating="true"
419
:loadTilesWhileInteracting="true"
@@ -12,7 +27,11 @@
1227
:constrainRotation="16"
1328
/>
1429

15-
<ol-vector-layer background="#1a2b39" ref="vectorSourceRef">
30+
<ol-vector-layer
31+
background="#1a2b39"
32+
ref="vectorSourceRef"
33+
:opacity="opacity"
34+
>
1635
<ol-source-vector :url="url" :format="geoJson">
1736
<ol-style :overrideStyleFunction="styleFn"></ol-style>
1837
</ol-source-vector>
@@ -35,6 +54,7 @@ import { shiftKeyOnly } from "ol/events/condition";
3554
import { ref, inject } from "vue";
3655
import type { DragBoxEvent } from "ol/interaction/DragBox";
3756
57+
const opacity = ref(1);
3858
const center = ref([0, 0]);
3959
const projection = ref("EPSG:4326");
4060
const zoom = ref(0);

0 commit comments

Comments
 (0)