Skip to content

Commit 75b62dd

Browse files
committed
refactor(ol-source-cluster): migrate to script setup
1 parent d26df84 commit 75b62dd

File tree

5 files changed

+119
-137
lines changed

5 files changed

+119
-137
lines changed

docs/componentsguide/sources/cluster/index.md

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ol-source-cluster
22

3-
Layer source to cluster vector data. Works out of the box with point geometries. For other geometry types, or if not all geometries should be considered for clustering, a custom geometryFunction can be defined.
3+
Layer source to cluster vector data.
4+
Works out of the box with point geometries.
5+
For other geometry types, or if not all geometries should be considered for clustering, a custom geometryFunction can be defined.
46

57
<script setup>
68
import ClusterDemo from "@demos/ClusterDemo.vue"
@@ -21,7 +23,7 @@ This example shows how to do clustering on 1000 point features.
2123
ref="map"
2224
:loadTilesWhileAnimating="true"
2325
:loadTilesWhileInteracting="true"
24-
style="height:800px"
26+
style="height: 800px"
2527
>
2628
<ol-view
2729
ref="view"
@@ -38,7 +40,7 @@ This example shows how to do clustering on 1000 point features.
3840
<ol-vector-layer>
3941
<ol-source-cluster :distance="40">
4042
<ol-source-vector>
41-
<ol-feature v-for="index in 1000" :key="index">
43+
<ol-feature v-for="index in 300" :key="index">
4244
<ol-geom-point
4345
:coordinates="[
4446
getRandomInRange(24, 45, 3),
@@ -65,38 +67,35 @@ This example shows how to do clustering on 1000 point features.
6567
</ol-map>
6668
</template>
6769
68-
<script>
69-
import { ref, inject } from "vue";
70-
71-
export default {
72-
setup() {
73-
const center = ref([34, 39.13]);
74-
const projection = ref("EPSG:4326");
75-
const zoom = ref(6.8);
76-
const rotation = ref(0);
77-
78-
const overrideStyleFunction = (feature, style) => {
79-
let clusteredFeatures = feature.get("features");
80-
let size = clusteredFeatures.length;
81-
82-
style.getText().setText(size.toString());
83-
};
84-
85-
const getRandomInRange = (from, to, fixed) => {
86-
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
87-
};
88-
89-
return {
90-
center,
91-
projection,
92-
zoom,
93-
rotation,
94-
overrideStyleFunction,
95-
getRandomInRange,
96-
};
97-
},
70+
<script setup>
71+
import { ref } from "vue";
72+
73+
const center = ref([34, 39.13]);
74+
const projection = ref("EPSG:4326");
75+
const zoom = ref(6.8);
76+
const rotation = ref(0);
77+
78+
const overrideStyleFunction = (feature, style) => {
79+
const clusteredFeatures = feature.get("features");
80+
const size = clusteredFeatures.length;
81+
82+
style.getText().setText(size.toString());
83+
};
84+
85+
const getRandomInRange = (from, to, fixed) => {
86+
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
9887
};
9988
</script>
89+
90+
<style>
91+
.overlay-content {
92+
background: red !important;
93+
color: white;
94+
box-shadow: 0 5px 10px rgb(2 2 2 / 20%);
95+
padding: 10px 20px;
96+
font-size: 16px;
97+
}
98+
</style>
10099
```
101100

102101
## Properties
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<template>
2+
<div>
3+
<slot></slot>
4+
</div>
5+
</template>
6+
7+
<script setup>
8+
import { Cluster } from "ol/source";
9+
10+
import { inject, watch, onMounted, onUnmounted, provide, computed } from "vue";
11+
12+
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
13+
14+
const props = defineProps({
15+
attributions: {
16+
type: [String, Array],
17+
},
18+
distance: {
19+
type: Number,
20+
default: 20,
21+
},
22+
geometryFunction: {
23+
type: Function,
24+
default: (feature) => feature.getGeometry(),
25+
},
26+
wrapX: {
27+
type: Boolean,
28+
default: true,
29+
},
30+
});
31+
32+
const layer = inject("vectorLayer");
33+
34+
const { properties } = usePropsAsObjectProperties(props);
35+
36+
const source = computed(() => {
37+
const c = new Cluster(properties);
38+
return c;
39+
});
40+
41+
const applySource = () => {
42+
layer.value.setSource(null);
43+
layer.value.setSource(source.value);
44+
layer.value.changed();
45+
};
46+
watch(properties, () => {
47+
applySource();
48+
});
49+
50+
watch(layer, () => {
51+
applySource();
52+
});
53+
54+
onMounted(() => {
55+
layer.value.setSource(source.value);
56+
layer.value.changed();
57+
});
58+
59+
onUnmounted(() => {
60+
layer.value.setSource(null);
61+
});
62+
63+
provide("vectorLayer", source);
64+
65+
defineExpose({
66+
layer,
67+
source,
68+
});
69+
</script>
70+
71+
<style lang=""></style>

src/components/sources/SourceCluster.vue

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/components/sources/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import OlSourceBingMaps from "./OlSourceBingMaps.vue";
2+
import OlSourceCluster from "./OlSourceCluster.vue";
13
import OlSourceXYZ from "./OlSourceXYZ.vue";
24
import OlSourceWMTS from "./OlSourceWMTS.vue";
35
import SourceOSM from "./SourceOSM.vue";
46
import SourceImageStatic from "./SourceImageStatic.vue";
57
import SourceVector from "./SourceVector.vue";
6-
import SourceCluster from "./SourceCluster.vue";
7-
import SourceBingMaps from "./SourceBingMaps.vue";
88
import SourceTianDiTu from "./SourceTianDiTu.vue";
99
import SourceImageWMS from "./SourceImageWMS.vue";
1010
import SourceTileWMS from "./SourceTileWMS.vue";
@@ -21,10 +21,10 @@ function install(app) {
2121
app.component("ol-source-bingmaps", OlSourceBingMaps);
2222
app.component("ol-source-xyz", OlSourceXYZ);
2323
app.component("ol-source-wmts", OlSourceWMTS);
24+
app.component("ol-source-cluster", OlSourceCluster);
2425
app.component(SourceOSM.name, SourceOSM);
2526
app.component(SourceImageStatic.name, SourceImageStatic);
2627
app.component(SourceVector.name, SourceVector);
27-
app.component(SourceCluster.name, SourceCluster);
2828
app.component(SourceTianDiTu.name, SourceTianDiTu);
2929
app.component(SourceImageWMS.name, SourceImageWMS);
3030
app.component(SourceTileWMS.name, SourceTileWMS);
@@ -37,12 +37,12 @@ export default install;
3737
export {
3838
install,
3939
OlSourceBingMaps,
40+
OlSourceCluster,
4041
OlSourceXYZ,
4142
OlSourceWMTS,
4243
SourceOSM,
4344
SourceImageStatic,
4445
SourceVector,
45-
SourceCluster,
4646
SourceTianDiTu,
4747
SourceImageWMS,
4848
SourceTileWMS,

src/demos/ClusterDemo.vue

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,23 @@
4747
</ol-map>
4848
</template>
4949

50-
<script>
50+
<script setup>
5151
import { ref } from "vue";
5252
53-
export default {
54-
setup() {
55-
const center = ref([34, 39.13]);
56-
const projection = ref("EPSG:4326");
57-
const zoom = ref(6.8);
58-
const rotation = ref(0);
53+
const center = ref([34, 39.13]);
54+
const projection = ref("EPSG:4326");
55+
const zoom = ref(6.8);
56+
const rotation = ref(0);
5957
60-
const overrideStyleFunction = (feature, style) => {
61-
const clusteredFeatures = feature.get("features");
62-
const size = clusteredFeatures.length;
58+
const overrideStyleFunction = (feature, style) => {
59+
const clusteredFeatures = feature.get("features");
60+
const size = clusteredFeatures.length;
6361
64-
style.getText().setText(size.toString());
65-
};
66-
67-
const getRandomInRange = (from, to, fixed) => {
68-
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
69-
};
62+
style.getText().setText(size.toString());
63+
};
7064
71-
return {
72-
center,
73-
projection,
74-
zoom,
75-
rotation,
76-
overrideStyleFunction,
77-
getRandomInRange,
78-
};
79-
},
65+
const getRandomInRange = (from, to, fixed) => {
66+
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
8067
};
8168
</script>
8269

0 commit comments

Comments
 (0)