Ogc Vector Tiles Geographic
Runnable OpenLayers example: Ogc Vector Tiles Geographic. 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 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;color:#16181d}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar input[type=range]{width:110px}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label for="lvl">层级 <b id="lv">5</b></label>
<input type="range" id="lvl" min="2" max="7" step="1" value="5">
<span>瓦片数 <b id="cnt">—</b> · 每块 <b id="span">—</b>°</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 (!window.ol) {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
return;
}
var fromLonLat = ol.proj.fromLonLat;
/* 度制网格:z0 覆盖全球 2×1 块,每升一级边长减半 */
function tileSpan(z) { return 180 / Math.pow(2, z); }
function featuresInTile(z, tx, ty) {
var span = tileSpan(z);
var lon0 = -180 + tx * span;
var lat0 = 90 - (ty + 1) * span;
var out = [];
/* 同一瓦片内用固定种子生成要素,保证重复渲染结果一致 */
function seed(i) {
var h = Math.abs((tx * 7919 + ty * 104729 + z * 1299709 + i * 15485863) % 2147483647);
return (h % 997) / 997;
}
for (var i = 0; i < 4; i++) {
var lon = lon0 + seed(i) * span;
var lat = lat0 + seed(i + 5) * span;
out.push({
lonlat: [lon, lat],
kind: seed(i + 9) > 0.5 ? 'a' : 'b'
});
}
return out;
}
var source = new ol.source.Vector();
var boundsSource = new ol.source.Vector();
var layer = new ol.layer.Vector({ source: source });
var boundsLayer = new ol.layer.Vector({ source: boundsSource, zIndex: 20 });
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'}) }),
boundsLayer,
layer
],
view: new ol.View({ center: fromLonLat([110, 32]), zoom: 4 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
var level = 5;
function rebuild() {
source.clear();
boundsSource.clear();
var span = tileSpan(level);
var lonMin = 60, lonMax = 150, latMin = 0, latMax = 60;
var x0 = Math.floor((lonMin + 180) / span), x1 = Math.ceil((lonMax + 180) / span);
var y0 = Math.floor((90 - latMax) / span), y1 = Math.ceil((90 - latMin) / span);
var total = 0;
for (var tx = x0; tx < x1; tx++) {
for (var ty = y0; ty < y1; ty++) {
var lon0 = -180 + tx * span, lat0 = 90 - (ty + 1) * span;
var ring = [[lon0, lat0], [lon0 + span, lat0], [lon0 + span, lat0 + span], [lon0, lat0 + span], [lon0, lat0]]
.map(function (c) { return fromLonLat(c); });
var bf = new ol.Feature({ geometry: new ol.geom.Polygon([ring]) });
bf.setStyle(new ol.style.Style({
fill: new ol.style.Fill({ color: 'rgba(31,95,208,0.05)' }),
stroke: new ol.style.Stroke({ color: 'rgba(31,95,208,0.4)', width: 1 }),
text: new ol.style.Text({
text: level + '/' + tx + '/' + ty,
font: '500 10px ui-monospace,monospace',
fill: new ol.style.Fill({ color: '#0b3d91' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 2.5 })
})
}));
boundsSource.addFeature(bf);
/* 官方在 4326 视图里直接挂矢量瓦片层;这里底图固定在 3857,
所以把瓦片要素用 fromLonLat 换算到视图投影后渲染 */
featuresInTile(level, tx, ty).forEach(function (item) {
var f = new ol.Feature({ geometry: new ol.geom.Point(fromLonLat(item.lonlat)) });
f.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({
color: item.kind === 'a' ? 'rgba(31,95,208,0.85)' : 'rgba(47,158,110,0.85)'
}),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 1.5 })
})
}));
source.addFeature(f);
total++;
});
}
}
document.getElementById('lv').textContent = level;
document.getElementById('cnt').textContent = source.getFeatures().length;
document.getElementById('span').textContent = span.toFixed(3);
}
document.getElementById('lvl').addEventListener('input', function () {
level = Number(this.value);
rebuild();
});
rebuild();
})();
</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.