WMS Tiled Wrap 180
Runnable OpenLayers example: WMS Tiled Wrap 180. 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 WMS 跨 180 度
要点:TileWMS 的 wrapX 默认开启,世界副本在 180 度经线两侧连续显示
依赖: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>WMS 跨 180 度</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>wrapX: true</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;
}
/* ---- 页面内合成的世界轮廓,用于跨 180 度与瓦片顺序演示 ---- */
var WORLD_BLOCKS = [
{name: '亚洲', color: '#b6d7a8', pts: [[60, 5], [145, 5], [145, 60], [60, 60]]},
{name: '欧洲', color: '#9fc5e8', pts: [[-10, 35], [60, 35], [60, 70], [-10, 70]]},
{name: '非洲', color: '#ffe599', pts: [[-20, -35], [50, -35], [50, 35], [-20, 35]]},
{name: '美洲', color: '#f4b183', pts: [[-170, -20], [-50, -20], [-50, 60], [-170, 60]]}
];
function worldParam(src, name) {
var m = new RegExp('[?&]' + name + '=([^&]*)').exec(src || '');
return m ? decodeURIComponent(m[1].replace(/\+/g, ' ')) : '';
}
function worldDraw(ctx, w, h, bbox) {
var x0 = bbox[0], y0 = bbox[1], x1 = bbox[2], y1 = bbox[3];
ctx.fillStyle = '#dbe9f4';
ctx.fillRect(0, 0, w, h);
function px(p) {
var c = ol.proj.fromLonLat(p);
return [(c[0] - x0) / (x1 - x0) * w, (y1 - c[1]) / (y1 - y0) * h];
}
WORLD_BLOCKS.forEach(function (block) {
var pts = block.pts.map(px);
ctx.beginPath();
ctx.moveTo(pts[0][0], pts[0][1]);
for (var i = 1; i < pts.length; ++i) {
ctx.lineTo(pts[i][0], pts[i][1]);
}
ctx.closePath();
ctx.globalAlpha = 0.8;
ctx.fillStyle = block.color;
ctx.fill();
ctx.globalAlpha = 1;
ctx.strokeStyle = '#6c7d8c';
ctx.stroke();
if (w >= 200) {
ctx.fillStyle = '#2f3d4a';
ctx.font = '500 12px sans-serif';
ctx.textAlign = 'center';
ctx.fillText(block.name, (pts[0][0] + pts[2][0]) / 2, (pts[0][1] + pts[2][1]) / 2);
}
});
ctx.strokeStyle = 'rgba(80, 95, 110, 0.25)';
ctx.strokeRect(0.5, 0.5, w - 1, h - 1);
}
function worldToURL(w, h, bbox) {
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
worldDraw(canvas.getContext('2d'), w, h, bbox);
return canvas.toDataURL('image/png');
}
var wmsSource = new ol.source.TileWMS({
url: 'https://demo.invalid/wms',
params: {LAYERS: 'demo:world', TILED: true},
serverType: 'geoserver',
wrapX: true,
transition: 0,
tileLoadFunction: function (tile, src) {
var bbox = worldParam(src, 'BBOX').split(',').map(Number);
var width = parseInt(worldParam(src, 'WIDTH'), 10) || 256;
var height = parseInt(worldParam(src, 'HEIGHT'), 10) || 256;
if (bbox.length === 4) {
tile.getImage().src = worldToURL(width, height, bbox);
}
}
});
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.55}),
new ol.layer.Tile({source: wmsSource, opacity: 0.75})
],
view: new ol.View({
center: ol.proj.fromLonLat([179, 12]),
zoom: 2
})
});
map.on('moveend', function () {
var center = ol.proj.toLonLat(map.getView().getCenter());
document.getElementById('info').textContent =
'中心经度 ' + center[0].toFixed(2) + '° · wrapX true';
});
})();
</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.