Skip to content

Commit a90d4eb

Browse files
committed
refactor(ol-tile-layer): migrate to script setup
1 parent 9974f43 commit a90d4eb

File tree

4 files changed

+86
-106
lines changed

4 files changed

+86
-106
lines changed

docs/componentsguide/layers/tilelayer/index.md

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Example below shows how to use ol-layer-tile component together with ol-source-w
1818
<ol-map
1919
:loadTilesWhileAnimating="true"
2020
:loadTilesWhileInteracting="true"
21-
style="height:400px"
21+
style="height: 400px"
2222
>
2323
<ol-view ref="view" :center="center" :rotation="rotation" :zoom="zoom" />
2424
@@ -39,37 +39,20 @@ Example below shows how to use ol-layer-tile component together with ol-source-w
3939
</ol-map>
4040
</template>
4141
42-
<script>
42+
<script setup>
4343
import { ref } from "vue";
44-
export default {
45-
setup() {
46-
const center = ref([-90, 50]);
47-
const zoom = ref(2);
48-
const rotation = ref(0);
49-
const url = ref(
50-
"https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/WMTS/"
51-
);
52-
const layerName = ref("0");
53-
const matrixSet = ref("EPSG:3857");
54-
const format = ref("image/png");
55-
const styleName = ref("default");
56-
const attribution = ref(
57-
'Tiles © <a href="https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/">ArcGIS</a>'
58-
);
59-
60-
return {
61-
center,
62-
zoom,
63-
rotation,
64-
url,
65-
layerName,
66-
matrixSet,
67-
format,
68-
styleName,
69-
attribution,
70-
};
71-
},
72-
};
44+
45+
const center = ref([-11158582, 4813697]);
46+
const zoom = ref(4);
47+
const rotation = ref(0);
48+
const url = ref("https://mrdata.usgs.gov/mapcache/wmts");
49+
const layerName = ref("sgmc2");
50+
const matrixSet = ref("GoogleMapsCompatible");
51+
const format = ref("image/png");
52+
const styleName = ref("default");
53+
const attribution = ref(
54+
'Tiles © <a href="https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/">ArcGIS</a>'
55+
);
7356
</script>
7457
```
7558

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<template lang="">
2+
<div>
3+
<slot></slot>
4+
</div>
5+
</template>
6+
7+
<script setup>
8+
import { inject, provide, onUnmounted, onMounted, watch, computed } from "vue";
9+
10+
import TileLayer from "ol/layer/Tile";
11+
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
12+
import useBaseLayerProps from "@/composables/useBaseLayerProps";
13+
14+
const props = defineProps({
15+
...useBaseLayerProps(),
16+
preload: {
17+
type: Number,
18+
default: 1,
19+
},
20+
});
21+
22+
const map = inject("map");
23+
const overViewMap = inject("overviewMap", null);
24+
25+
const { properties } = usePropsAsObjectProperties(props);
26+
27+
const tileLayer = computed(() => new TileLayer(properties));
28+
29+
const applyTileLayer = () => {
30+
if (overViewMap != null) {
31+
overViewMap.value.getOverviewMap().addLayer(tileLayer.value);
32+
overViewMap.value.changed();
33+
} else {
34+
map.addLayer(tileLayer.value);
35+
}
36+
};
37+
38+
const removeTileLayer = () => {
39+
if (overViewMap != null) {
40+
overViewMap.value.getOverviewMap().removeLayer(tileLayer.value);
41+
overViewMap.value.changed();
42+
} else {
43+
map.removeLayer(tileLayer.value);
44+
}
45+
};
46+
47+
if (overViewMap != null) {
48+
watch(overViewMap, () => {
49+
removeTileLayer();
50+
applyTileLayer();
51+
});
52+
}
53+
54+
onMounted(() => {
55+
applyTileLayer();
56+
});
57+
58+
onUnmounted(() => {
59+
removeTileLayer();
60+
});
61+
62+
provide("tileLayer", tileLayer);
63+
64+
defineExpose({
65+
tileLayer,
66+
});
67+
</script>
68+
69+
<style lang=""></style>

src/components/layers/TileLayer.vue

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

src/components/layers/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import TileLayer from "./TileLayer.vue";
21
import WebGLTileLayer from "./WebGLTileLayer.vue";
32
import VectorImageLayer from "./VectorImageLayer.vue";
43
import VectorLayer from "./VectorLayer.vue";
54
import OlAnimatedClusterLayer from "./OlAnimatedClusterLayer.vue";
65
import OlHeatmapLayer from "./OlHeatmapLayer.vue";
76
import OlImageLayer from "./OlImageLayer.vue";
7+
import OlTileLayer from "./OlTileLayer.vue";
88
import WebglPointsLayer from "./WebglPointsLayer.vue";
99

1010
function install(app) {
@@ -17,7 +17,7 @@ function install(app) {
1717
app.component("ol-animated-clusterlayer", OlAnimatedClusterLayer);
1818
app.component("ol-heatmap-layer", OlHeatmapLayer);
1919
app.component("ol-image-layer", OlImageLayer);
20-
app.component(TileLayer.name, TileLayer);
20+
app.component("ol-tile-layer", OlTileLayer);
2121
app.component(WebGLTileLayer.name, WebGLTileLayer);
2222
app.component(VectorImageLayer.name, VectorImageLayer);
2323
app.component(VectorLayer.name, VectorLayer);
@@ -31,7 +31,7 @@ export {
3131
OlAnimatedClusterLayer,
3232
OlHeatmapLayer,
3333
OlImageLayer,
34-
TileLayer,
34+
OlTileLayer,
3535
WebGLTileLayer,
3636
VectorImageLayer,
3737
VectorLayer,

0 commit comments

Comments
 (0)