# 七.案例 7
# 1.在屏幕外画布中渲染的矢量瓦片
查看代码详情
<template>
<div ref="map" class="map">
<pre id="info" class="info" />
</div>
</template>
<script>
export default {
mounted() {
let {
Map,
View,
layer: { Layer },
source: { Source },
control: { FullScreen },
tilegrid: { createXYZ },
} = ol;
const worker = new Worker("./worker.js", { type: "module" });
let container,
transformContainer,
canvas,
rendering,
workerFrameState,
mainThreadFrameState;
function updateContainerTransform() {
if (workerFrameState) {
const viewState = mainThreadFrameState.viewState;
const renderedViewState = workerFrameState.viewState;
const center = viewState.center;
const resolution = viewState.resolution;
const rotation = viewState.rotation;
const renderedCenter = renderedViewState.center;
const renderedResolution = renderedViewState.resolution;
const renderedRotation = renderedViewState.rotation;
const transform = create();
if (!rotation) {
compose(
transform,
(renderedCenter[0] - center[0]) / resolution,
(center[1] - renderedCenter[1]) / resolution,
renderedResolution / resolution,
renderedResolution / resolution,
rotation - renderedRotation,
0,
0
);
}
transformContainer.style.transform = toTransformString(transform);
}
}
const map = new Map({
layers: [
new Layer({
render: function (frameState) {
if (!container) {
container = document.createElement("div");
container.style.position = "absolute";
container.style.width = "100%";
container.style.height = "100%";
transformContainer = document.createElement("div");
transformContainer.style.position = "absolute";
transformContainer.style.width = "100%";
transformContainer.style.height = "100%";
container.appendChild(transformContainer);
canvas = document.createElement("canvas");
canvas.style.position = "absolute";
canvas.style.left = "0";
canvas.style.transformOrigin = "top left";
transformContainer.appendChild(canvas);
}
mainThreadFrameState = frameState;
updateContainerTransform();
if (!rendering) {
rendering = true;
worker.postMessage({
action: "render",
frameState: JSON.parse(stringify(frameState)),
});
} else {
frameState.animate = true;
}
return container;
},
source: new Source({
attributions: [
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a>',
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>',
],
}),
}),
],
target: this.$refs.map,
view: new View({
resolutions: createXYZ({ tileSize: 512 }).getResolutions(),
center: [12579156, 3274244],
zoom: 2,
}),
});
map.addControl(new FullScreen());
map.on("pointermove", function (evt) {
if (evt.dragging) {
return;
}
const pixel = map.getEventPixel(evt.originalEvent);
worker.postMessage({
action: "requestFeatures",
pixel: pixel,
});
});
worker.addEventListener("message", (message) => {
if (message.data.action === "loadImage") {
const image = new Image();
image.crossOrigin = "anonymous";
image.addEventListener("load", function () {
createImageBitmap(image, 0, 0, image.width, image.height).then(
(imageBitmap) => {
worker.postMessage(
{
action: "imageLoaded",
image: imageBitmap,
src: message.data.src,
},
[imageBitmap]
);
}
);
});
image.src = message.data.src;
} else if (message.data.action === "getFeatures") {
showInfo(message.data.features);
} else if (message.data.action === "requestRender") {
map.render();
} else if (canvas && message.data.action === "rendered") {
requestAnimationFrame(function () {
const imageData = message.data.imageData;
canvas.width = imageData.width;
canvas.height = imageData.height;
canvas.getContext("2d").drawImage(imageData, 0, 0);
canvas.style.transform = message.data.transform;
workerFrameState = message.data.frameState;
updateContainerTransform();
});
rendering = false;
}
});
const info = document.getElementById("info");
function showInfo(propertiesFromFeatures) {
if (propertiesFromFeatures.length == 0) {
info.innerText = "";
info.style.opacity = 0;
return;
}
const properties = propertiesFromFeatures.map((e) =>
Object.keys(e)
.filter((key) => !key.includes(":"))
.reduce(
(newObj, currKey) => ((newObj[currKey] = e[currKey]), newObj),
{}
)
);
info.innerText = JSON.stringify(properties, null, 2);
info.style.opacity = 1;
}
},
};
</script>
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 2.查看 Min-Zoom
查看代码详情
<template>
<div ref="map" class="map"></div>
</template>
<script>
export default {
mounted() {
let {
Map,
View,
layer: { Tile: TileLayer },
source: { OSM },
} = ol
const viewport = this.$refs.map
function getMinZoom() {
const width = viewport.clientWidth
return Math.ceil(Math.LOG2E * Math.log(width / 256))
}
const initialZoom = getMinZoom()
const view = new View({
center: [12579156, 3274244],
minZoom: initialZoom,
zoom: initialZoom,
})
const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
],
target: this.$refs.map,
view: view,
})
window.addEventListener("resize", function () {
const minZoom = getMinZoom()
if (minZoom !== view.getMinZoom()) {
view.setMinZoom(minZoom)
}
})
},
}
</script>
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
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
# 3.查看填充物
查看代码详情
<template>
<div>
<div class="mapcontainer">
<div ref="map" class="map"></div>
<div class="padding-top"></div>
<div class="padding-left"></div>
<div class="padding-right"></div>
<div class="padding-bottom"></div>
</div>
<button ref="zoomtoswitzerland">移动到瑞士</button>
<button ref="centerlausanne">瑞士洛桑中心</button>
</div>
</template>
<script>
export default {
mounted() {
let {
format: { GeoJSON },
Map,
View,
layer: { Tile: TileLayer, Vector: VectorLayer },
source: { OSM, Vector: VectorSource },
style: { Circle: CircleStyle, Fill, Stroke, Style },
proj: { fromLonLat },
} = ol;
const source = new VectorSource({
url: this.$withBase("/data/geojson/switzerland.geojson"),
format: new GeoJSON(),
});
const style = new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.6)",
}),
stroke: new Stroke({
color: "#319FD3",
width: 1,
}),
image: new CircleStyle({
radius: 5,
fill: new Fill({
color: "rgba(255, 255, 255, 0.6)",
}),
stroke: new Stroke({
color: "#319FD3",
width: 1,
}),
}),
});
const vectorLayer = new VectorLayer({
source: source,
style: style,
});
const view = new View({
center: fromLonLat([6.6339863, 46.5193823]),
padding: [170, 50, 30, 150],
zoom: 6,
});
const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
vectorLayer,
],
target: this.$refs.map,
view: view,
});
vectorLayer.getSource().on(
"featuresloadend",
function () {
this.$refs.zoomtoswitzerland.addEventListener(
"click",
function () {
const feature = source.getFeatures()[0];
const polygon = feature.getGeometry();
view.fit(polygon);
},
false
);
this.$refs.centerlausanne.addEventListener(
"click",
function () {
const feature = source.getFeatures()[1];
const point = feature.getGeometry();
view.setCenter(point.getCoordinates());
},
false
);
}.bind(this)
);
},
};
</script>
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93