把经纬度转到墨卡托,再映射到 Three.js 世界坐标,才能把行政区轮廓挤出到地图上。
1 2 3 4 5 6 7 8 9 10 11
| function lonLat2Mercator(E, N) { var x = E * 20037508.34 / 180; var y = Math.log(Math.tan((90 + N) * Math.PI / 360)) / (Math.PI / 180); y = y * 20037508.34 / 180; return { x: x, y: y, } } export { lonLat2Mercator };
|
经纬度 → 地图投影坐标 → Three.js 世界坐标。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import {lonLat2Mercator} from './lonLat2Mercator.js';
function ExtrudeMesh(pointsArrs, height) { var shapeArr = []; pointsArrs.forEach(pointsArr => { var vector2Arr = []; pointsArr[0].forEach(elem => { var coord = lonLat2Mercator(elem[0], elem[1]) vector2Arr.push(new THREE.Vector2(coord.x, coord.y)) }); var shape = new THREE.Shape(vector2Arr); shapeArr.push(shape); }); var material = new THREE.MeshLambertMaterial({ color: 0x004444, }); var geometry = new THREE.ExtrudeBufferGeometry( shapeArr, { depth: height, bevelEnabled: false } ); var mesh = new THREE.Mesh(geometry, material); return mesh; } export { ExtrudeMesh };
|
GeoJSON 经纬度
↓
Web Mercator 投影
↓
平面 x / y
↓
THREE.Vector2
↓
THREE.Shape
↓
ExtrudeGeometry 拉伸
↓
3D 行政区 Mesh