forked from MelihAltintas/vue3-openlayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOlSourceVectorTile.vue
70 lines (57 loc) · 1.5 KB
/
OlSourceVectorTile.vue
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
<template>
<div>
<slot></slot>
</div>
</template>
<script setup lang="ts">
import VectorTileSource, { type Options } from "ol/source/VectorTile";
import type VectorTileLayer from "ol/layer/VectorTile";
import type { Ref } from "vue";
import { inject, watch, onMounted, onUnmounted, provide, computed } from "vue";
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
import {
useOpenLayersEvents,
FEATURE_EVENTS,
} from "@/composables/useOpenLayersEvents";
// prevent warnings caused by event pass-through via useOpenLayersEvents composable
defineOptions({
inheritAttrs: false,
});
const props = withDefaults(defineProps<Options>(), {
overlaps: true,
projection: "EPSG:3857",
wrapX: true,
});
const vectorTileLayer = inject<Ref<VectorTileLayer> | null>(
"vectorTileLayer",
null
);
const { properties } = usePropsAsObjectProperties(props);
const source = computed(() => new VectorTileSource(properties));
useOpenLayersEvents(source, FEATURE_EVENTS);
const applySource = () => {
vectorTileLayer?.value?.setSource(null);
vectorTileLayer?.value?.setSource(source.value);
vectorTileLayer?.value?.changed();
};
watch(properties, () => {
applySource();
});
watch(
() => vectorTileLayer?.value,
() => {
applySource();
}
);
onMounted(() => {
vectorTileLayer?.value?.setSource(source.value);
});
onUnmounted(() => {
vectorTileLayer?.value?.setSource(null);
});
provide("vectorSource", source);
defineExpose({
vectorTileLayer,
source,
});
</script>