Reprojection By Code
Runnable OpenLayers example: Reprojection By Code. 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
<!--
示例:代码注册投影
要点:ol.proj.addProjection 加 addCoordinateTransforms,自定义投影即可参与转换
依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
替代:官方用 proj4 定义 EPSG:21781,这里用球面 Lambert 方位等积公式自行实现
-->
<!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 b{color:#16181d;font-weight:600;font-variant-numeric:tabular-nums}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
table.mono td{font-family:ui-monospace,Consolas,monospace;font-size:11px}
</style>
</head>
<body>
<div class="bar">
<span>自定义投影 <b>LOCAL:LAEA</b></span>
<span>坐标转换 <b id="cv">—</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 R = 6371008.8;
var D2R = Math.PI / 180;
/* 球面 Lambert 方位等积投影,原点取东经 116°、北纬 39° */
var LON0 = 116 * D2R;
var LAT0 = 39 * D2R;
function forward(coord) {
var lon = coord[0] * D2R;
var lat = coord[1] * D2R;
var k = Math.sqrt(2 / (1 + Math.sin(LAT0) * Math.sin(lat) +
Math.cos(LAT0) * Math.cos(lat) * Math.cos(lon - LON0)));
return [
R * k * Math.cos(lat) * Math.sin(lon - LON0),
R * k * (Math.cos(LAT0) * Math.sin(lat) -
Math.sin(LAT0) * Math.cos(lat) * Math.cos(lon - LON0))
];
}
var custom = new ol.proj.Projection({
code: 'LOCAL:LAEA',
units: 'm',
extent: [-4e6, -4e6, 4e6, 4e6],
global: false
});
ol.proj.addProjection(custom);
ol.proj.addCoordinateTransforms('EPSG:4326', custom, forward, null);
var toCustom = ol.proj.getTransform('EPSG:4326', 'LOCAL:LAEA');
var backToMap = ol.proj.getTransform('LOCAL:LAEA', 'EPSG:3857');
var PTS = [
['北京', 116.391, 39.907],
['天津', 117.201, 39.084],
['石家庄', 114.515, 38.043],
['济南', 117.120, 36.651],
['太原', 112.549, 37.871]
];
var features = PTS.map(function (p) {
/* 先在自定义投影里算出坐标,再转回地图投影落图 */
var local = toCustom([p[1], p[2]]);
var xy = backToMap(local);
var f = new ol.Feature({
geometry: new ol.geom.Point(xy),
name: p[0],
local: local
});
f.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({ color: '#2f8f5b' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 2 })
}),
text: new ol.style.Text({
text: p[0],
offsetY: -14,
font: '500 12px sans-serif',
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 3 })
})
}));
return f;
});
var layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: features }) });
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.2, 38.6]), zoom: 6 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
var first = features[0].get('local');
document.getElementById('cv').textContent =
'北京 → ' + first[0].toFixed(0) + ', ' + first[1].toFixed(0) + ' m';
map.on('singleclick', function (evt) {
var ll = ol.proj.toLonLat(evt.coordinate);
var local = toCustom(ll);
document.getElementById('cv').textContent =
ll[0].toFixed(3) + ',' + ll[1].toFixed(3) + ' → ' +
local[0].toFixed(0) + ', ' + local[1].toFixed(0) + ' m';
});
})();
</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.