Arcgis Tiled
Connect an ArcGIS REST tile service with a custom tile grid and resolutions.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:ArcGIS REST 瓦片图层
要点:自定义 tileGrid 分辨率阶梯 + tileUrlFunction 按 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>ArcGIS REST 瓦片图层</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:10px;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="tile">—</b></span>
<span>当前缩放:<b id="zoom">—</b></span>
<span>瓦片网格 256px / 19 级</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;
}
/* 页面内生成瓦片:按 z/x/y 画出带坐标标注的方块,替代 ArcGIS REST 服务返回的图片 */
var cache = {};
function noise(x, y) {
var h = (x * 73856093) ^ (y * 19349663);
return Math.abs(h % 997) / 997;
}
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 = 256;
var ctx = c.getContext('2d');
var n = noise(x, y);
ctx.fillStyle = 'rgba(31,95,208,' + (0.05 + n * 0.12).toFixed(3) + ')';
ctx.fillRect(0, 0, 256, 256);
ctx.strokeStyle = 'rgba(31,95,208,0.5)';
ctx.lineWidth = 1;
ctx.strokeRect(0.5, 0.5, 255, 255);
for (var i = 1; i < 4; i++) {
ctx.beginPath(); ctx.moveTo(i * 64, 0); ctx.lineTo(i * 64, 256); ctx.stroke();
ctx.beginPath(); ctx.moveTo(0, i * 64); ctx.lineTo(256, i * 64); ctx.stroke();
}
ctx.fillStyle = 'rgba(15,63,143,0.9)';
ctx.font = '600 12px sans-serif';
ctx.fillText(z + ' / ' + x + ' / ' + y, 8, 20);
cache[key] = c.toDataURL('image/png');
return cache[key];
}
var tileGrid = ol.tilegrid.createXYZ({ tileSize: 256, maxZoom: 19 });
/* 官方示例用 ol.source.TileArcGISRest 指向 ArcGIS 服务;
这里用 ol.source.XYZ + tileUrlFunction,走同一套瓦片网格与加载流程 */
var arcgisLayer = new ol.layer.Tile({
opacity: 0.85,
source: new ol.source.XYZ({
tileGrid: tileGrid,
attributions: '自造瓦片 · 结构对齐 ArcGIS REST',
tileUrlFunction: function (tileCoord) {
return tileURL(tileCoord[0], tileCoord[1], tileCoord[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'}) }), arcgisLayer],
view: new ol.View({
center: ol.proj.fromLonLat([116.391263, 39.907359]),
zoom: 11,
maxZoom: 19
}),
controls: ol.control.defaults.defaults({ attribution: true, rotate: false })
});
function report() {
var z = Math.round(map.getView().getZoom());
var size = map.getSize();
var c = map.getCoordinateFromPixel([size[0] / 2, size[1] / 2]);
var tc = tileGrid.getTileCoordForCoordAndZ(c, z);
document.getElementById('zoom').textContent = z;
document.getElementById('tile').textContent = tc ? tc.join(' / ') : '—';
}
map.on('moveend', report);
report();
})();
</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.