Vector Tiles 4326
Runnable OpenLayers example: Vector Tiles 4326. 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 4326 矢量瓦片
要点:在 EPSG:4326 视图下使用自建瓦片网格,瓦片要素直接以经纬度范围生成
依赖: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>4326 矢量瓦片</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">
<span>EPSG:4326 瓦片网格 · tileSize 512</span>
<span class="sep"></span>
<span id="info">—</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 tileGrid = ol.tilegrid.createXYZ({
extent: [-180, -90, 180, 90],
tileSize: 512,
maxResolution: 180 / 512,
maxZoom: 13
});
var tileFormat = {
getType: function () {
return 'arraybuffer';
},
readProjection: function () {
return 'EPSG:4326';
},
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]
];
}
return [
new ol.Feature({
geometry: new ol.geom.Polygon([box(0.12, 0.20, 0.48, 0.78)]),
name: '经纬网区块 A',
tile: info.z + '/' + info.x + '/' + info.y
}),
new ol.Feature({
geometry: new ol.geom.Polygon([box(0.56, 0.12, 0.90, 0.66)]),
name: '经纬网区块 B',
tile: info.z + '/' + info.x + '/' + info.y
})
];
}
};
var source = new ol.source.VectorTile({
format: tileFormat,
projection: 'EPSG:4326',
tileGrid: tileGrid,
tileUrlFunction: function (tileCoord) {
return 'data:application/json;charset=utf-8,' + encodeURIComponent(
JSON.stringify({z: tileCoord[0], x: tileCoord[1], y: tileCoord[2]})
);
},
transition: 0
});
var layer = new ol.layer.VectorTile({
source: source,
style: function (feature) {
return new ol.style.Style({
fill: new ol.style.Fill({color: 'rgba(46, 125, 50, 0.35)'}),
stroke: new ol.style.Stroke({color: 'rgba(255, 255, 255, 0.85)', width: 1}),
text: new ol.style.Text({
text: feature.get('name'),
font: '500 12px sans-serif',
fill: new ol.style.Fill({color: '#15341a'}),
stroke: new ol.style.Stroke({color: '#ffffff', width: 3})
})
});
}
});
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.45}),
layer
],
view: new ol.View({
projection: 'EPSG:4326',
center: ol.proj.fromLonLat([116.391, 39.907], 'EPSG:4326'),
zoom: 4
})
});
map.on('pointermove', function (evt) {
if (evt.dragging) {
return;
}
var features = map.getFeaturesAtPixel(evt.pixel);
document.getElementById('info').textContent = features.length
? features[0].get('name') + ' · 瓦片 ' + features[0].get('tile')
: '—';
});
})();
</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.