Sphere Mollweide
Runnable OpenLayers example: Sphere Mollweide. 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
<!--
示例:球面摩尔威投影
要点:Mollweide 公式把经纬度投到等积椭圆世界图,纬度用迭代解出
依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
替代:官方用 proj4 注册全球投影,这里直接用公式在离屏 canvas 上绘制
-->
<!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}
.wrap{position:relative;flex:1 1 auto;min-height:0}
#map{position:absolute;inset:0;background:#eef0f3}
.panel{position:absolute;right:12px;bottom:12px;background:#fff;border:1px solid #e3e6ea;
border-radius:8px;box-shadow:0 4px 16px rgba(16,24,40,.12);padding:8px}
.panel h5{margin:0 0 6px;font-size:12px;color:#58606e;font-weight:600}
.panel canvas{display:block;border-radius:4px}
</style>
</head>
<body>
<div class="bar">
<span>投影 <b>Mollweide</b></span>
<span>红点为北京,绿线为每 30° 经纬网</span>
</div>
<div class="wrap">
<div id="map"></div>
<div class="panel">
<h5>等积椭圆世界图</h5>
<canvas id="world" width="320" height="180"></canvas>
</div>
</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;
/* Mollweide 正解:θ 由 2θ + sin2θ = π sinφ 迭代得到 */
function mollweide(lon, lat) {
var phi = lat * D2R;
var theta = phi;
for (var i = 0; i < 12; i++) {
var denom = 2 + 2 * Math.cos(2 * theta);
if (Math.abs(denom) < 1e-9) break;
theta -= (2 * theta + Math.sin(2 * theta) - Math.PI * Math.sin(phi)) / denom;
}
var k = 2 * Math.SQRT2 / Math.PI;
return [
R * k * (lon * D2R) * Math.cos(theta),
R * Math.SQRT2 * Math.sin(theta)
];
}
var canvas = document.getElementById('world');
var W = canvas.width;
var H = canvas.height;
var renderer = ol.render.toContext(canvas.getContext('2d'), {
size: [W, H],
pixelRatio: 1
});
var maxX = R * 2 * Math.SQRT2;
var maxY = R * Math.SQRT2;
function project(lon, lat) {
var p = mollweide(lon, lat);
return [
W / 2 + p[0] / maxX * (W - 16),
H / 2 - p[1] / maxY * (H - 16)
];
}
/* 经纬网 */
renderer.setStyle(new ol.style.Style({
stroke: new ol.style.Stroke({ color: 'rgba(47,143,91,.55)', width: 1 })
}));
for (var lonLine = -180; lonLine <= 180; lonLine += 30) {
var meridian = [];
for (var latLine = -90; latLine <= 90; latLine += 3) {
meridian.push(project(lonLine, latLine));
}
renderer.drawGeometry(new ol.geom.LineString(meridian));
}
for (var latLine2 = -60; latLine2 <= 60; latLine2 += 30) {
var parallel = [];
for (var lonLine2 = -180; lonLine2 <= 180; lonLine2 += 3) {
parallel.push(project(lonLine2, latLine2));
}
renderer.drawGeometry(new ol.geom.LineString(parallel));
}
/* 世界边界椭圆 */
renderer.setStyle(new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#8a92a1', width: 1.5 })
}));
renderer.drawGeometry(new ol.geom.Polygon([[
[W / 2 - (W - 16) / 2, H / 2],
[W / 2, H / 2 - (H - 16) / 2],
[W / 2 + (W - 16) / 2, H / 2],
[W / 2, H / 2 + (H - 16) / 2],
[W / 2 - (W - 16) / 2, H / 2]
]]));
/* 北京位置 */
var bj = project(116.391, 39.907);
renderer.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({ color: '#d64545' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 1.5 })
}),
text: new ol.style.Text({
text: '北京',
offsetY: -12,
font: '500 11px sans-serif',
fill: new ol.style.Fill({ color: '#d64545' })
})
}));
renderer.drawGeometry(new ol.geom.Point(bj));
/* 左侧地图仍用常规墨卡托视图,标注同一个城市 */
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'}) }),
new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([116.391, 39.907]))
})]
}),
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({ color: '#d64545' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 2 })
})
})
})
],
view: new ol.View({ center: ol.proj.fromLonLat([116.391, 39.907]), zoom: 4 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
})();
</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.