Vector Tile Selection
Runnable OpenLayers example: Vector Tile Selection. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:OpenLayers 矢量瓦片选择
要点:共享同一矢量瓦片源的选择图层,用要素 id 记录选中集合并重绘高亮
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>矢量瓦片选择</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>选择方式
<select id="type">
<option value="singleselect-click">单选(点击)</option>
<option value="singleselect-hover">单选(悬停)</option>
<option value="multiselect-click">多选(点击)</option>
</select>
</label>
<span class="sep"></span>
<span id="info">已选 0 个</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var selection = {};
var baseStyle = new ol.style.Style({
fill: new ol.style.Fill({color: 'rgba(40, 55, 70, 0.72)'}),
stroke: new ol.style.Stroke({color: 'rgba(220, 230, 240, 0.7)', width: 1})
});
var selectedStyle = new ol.style.Style({
fill: new ol.style.Fill({color: 'rgba(200, 30, 30, 0.45)'}),
stroke: new ol.style.Stroke({color: 'rgba(200, 30, 30, 0.95)', width: 2})
});
var tileFormat = {
getType: function () {
return 'arraybuffer';
},
readProjection: function () {
return 'EPSG:3857';
},
readFeatures: function (buffer, options) {
var info = JSON.parse(new TextDecoder().decode(buffer));
var extent = options.extent;
var w = extent[2] - extent[0];
var h = extent[3] - extent[1];
function box(x1, y1, x2, y2) {
return [
[extent[0] + x1 * w, extent[1] + y1 * h],
[extent[0] + x2 * w, extent[1] + y1 * h],
[extent[0] + x2 * w, extent[1] + y2 * h],
[extent[0] + x1 * w, extent[1] + y2 * h],
[extent[0] + x1 * w, extent[1] + y1 * h]
];
}
var features = [];
for (var i = 0; i < 3; ++i) {
var feature = new ol.Feature({
geometry: new ol.geom.Polygon([box(
0.10 + i * 0.28, 0.18 + (i % 2) * 0.20,
0.33 + i * 0.28, 0.62 + (i % 2) * 0.20
)]),
name: '要素 ' + (i + 1) + ' · ' + info.z + '/' + info.x + '/' + info.y
});
feature.setId(info.z + ':' + info.x + ':' + info.y + ':' + i);
features.push(feature);
}
return features;
}
};
var source = new ol.source.VectorTile({
format: tileFormat,
tileUrlFunction: function (tileCoord) {
return 'data:application/json;charset=utf-8,' + encodeURIComponent(
JSON.stringify({z: tileCoord[0], x: tileCoord[1], y: tileCoord[2]})
);
},
maxZoom: 14,
transition: 0
});
var vtLayer = new ol.layer.VectorTile({
source: source,
style: baseStyle
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}), opacity: 0.35}),
vtLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([116.391, 39.907]),
zoom: 11
})
});
var selectionLayer = new ol.layer.VectorTile({
map: map,
renderMode: 'vector',
source: source,
style: function (feature) {
return feature.getId() in selection ? selectedStyle : undefined;
}
});
var selectElement = document.getElementById('type');
function updateInfo() {
document.getElementById('info').textContent =
'已选 ' + Object.keys(selection).length + ' 个';
}
map.on(['click', 'pointermove'], function (event) {
if ((selectElement.value === 'singleselect-hover' && event.type !== 'pointermove') ||
(selectElement.value !== 'singleselect-hover' && event.type === 'pointermove')) {
return;
}
vtLayer.getFeatures(event.pixel).then(function (features) {
if (!features.length) {
selection = {};
selectionLayer.changed();
updateInfo();
return;
}
var feature = features[0];
var fid = feature.getId();
if (selectElement.value.indexOf('singleselect') === 0) {
selection = {};
}
selection[fid] = feature;
selectionLayer.changed();
updateInfo();
});
});
updateInfo();
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.