Getfeatureinfo Tile
Runnable OpenLayers example: Getfeatureinfo Tile. 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
<!--
示例:瓦片图层像元查询
要点:点击后由坐标定位到 z/x/y 与瓦片内像素,再从生成函数取值
依赖: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:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>瓦片:<b id="tzxy">—</b></span>
<span>瓦片内像素:<b id="px">—</b></span>
<span>属性值:<b id="val">—</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 SIZE = 256;
var cache = {};
/* 瓦片内属性:用 z/x/y 与像素位置共同决定的连续场 */
function sample(z, x, y, px, py) {
var u = (x + px / SIZE) / Math.pow(2, z);
var v = (y + py / SIZE) / Math.pow(2, z);
var t = 0.5 + 0.32 * Math.sin(u * 18.5) * Math.cos(v * 14.2) + 0.14 * Math.sin((u + v) * 31.0);
return {
t: t,
value: Math.round(120 + t * 880)
};
}
function tileURL(z, x, y) {
var key = z + '/' + x + '/' + y;
if (cache[key]) return cache[key];
var c = document.createElement('canvas');
c.width = c.height = SIZE;
var ctx = c.getContext('2d');
var img = ctx.createImageData(SIZE, SIZE);
for (var py = 0; py < SIZE; py++) {
for (var px = 0; px < SIZE; px++) {
var s = sample(z, x, y, px, py);
var t = Math.max(0, Math.min(1, s.t));
var i = (py * SIZE + px) * 4;
img.data[i] = Math.round(60 + t * 190);
img.data[i + 1] = Math.round(150 - t * 90);
img.data[i + 2] = Math.round(210 - t * 140);
img.data[i + 3] = 200;
}
}
ctx.putImageData(img, 0, 0);
ctx.strokeStyle = 'rgba(255,255,255,0.35)';
ctx.strokeRect(0.5, 0.5, SIZE - 1, SIZE - 1);
cache[key] = c.toDataURL('image/png');
return cache[key];
}
var tileGrid = ol.tilegrid.createXYZ({ tileSize: SIZE, maxZoom: 16 });
/* 官方示例向 WMS 发 GetFeatureInfo 请求;这里瓦片本身由页面生成,
点击后直接在本地按同样的 z/x/y + 像素定位算出属性 */
var layer = new ol.layer.Tile({
opacity: 0.85,
source: new ol.source.XYZ({
tileGrid: tileGrid,
attributions: '自造瓦片 · 查询逻辑对齐 GetFeatureInfo',
tileUrlFunction: function (tc) { return tileURL(tc[0], tc[1], tc[2]); }
})
});
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'}) }), layer],
view: new ol.View({ center: ol.proj.fromLonLat([116.391263, 39.907359]), zoom: 11, maxZoom: 16 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
map.on('singleclick', function (evt) {
var z = Math.round(map.getView().getZoom());
var tc = tileGrid.getTileCoordForCoordAndZ(evt.coordinate, z);
if (!tc) {
document.getElementById('tzxy').textContent = '—';
return;
}
var tileExtent = tileGrid.getTileCoordExtent(tc);
var px = Math.floor((evt.coordinate[0] - tileExtent[0]) / (tileExtent[2] - tileExtent[0]) * SIZE);
var py = Math.floor((tileExtent[3] - evt.coordinate[1]) / (tileExtent[3] - tileExtent[1]) * SIZE);
var s = sample(tc[0], tc[1], tc[2], px, py);
document.getElementById('tzxy').textContent = tc[0] + ' / ' + tc[1] + ' / ' + tc[2];
document.getElementById('px').textContent = px + ' , ' + py;
document.getElementById('val').textContent = s.value;
});
})();
</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.