Skip to content

WMS without 投影 ​

WMS without 投影示例。 WMS 投影

只要不需要进行坐标变换,OpenLayers 就可以很好地使用仅配置了 code 和 units 的投影。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import ScaleLine from 'ol/control/ScaleLine.js';
import {defaults as defaultControls} from 'ol/control/defaults.js';
import ImageLayer from 'ol/layer/Image.js';
import TileLayer from 'ol/layer/Tile.js';
import Projection from 'ol/proj/Projection.js';
import ImageWMS from 'ol/source/ImageWMS.js';
import TileWMS from 'ol/source/TileWMS.js';

const layers = [
  new TileLayer({
    source: new TileWMS({
      attributions:
        '© <a href="https://shop.swisstopo.admin.ch/en/products/maps/national/lk1000"' +
        'target="_blank">Pixelmap 1:1000000 / geo.admin.ch</a>',
      crossOrigin: 'anonymous',
      params: {
        'LAYERS': 'ch.swisstopo.pixelkarte-farbe-pk1000.noscale',
        'FORMAT': 'image/jpeg',
      },
      url: 'https://wms.geo.admin.ch/',
    }),
  }),
  new ImageLayer({
    source: new ImageWMS({
      attributions:
        '© <a href="https://www.hydrodaten.admin.ch/en/notes-on-the-flood-alert-maps.html"' +
        'target="_blank">Flood Alert / geo.admin.ch</a>',
      crossOrigin: 'anonymous',
      params: {'LAYERS': 'ch.bafu.hydroweb-warnkarte_national'},
      serverType: 'mapserver',
      url: 'https://wms.geo.admin.ch/',
    }),
  }),
];

// 一个最小化的投影对象只配置了 SRS 代码和地图单位。
// 这样的投影对象无法进行客户端坐标变换。请求瓦片只需要
// 投影代码加上一个笛卡尔坐标系的瓦片网格;这些坐标
// 如何对应经纬度并不重要。
//
// 由于没有可用的变换,必须假设投影单位表示真实距离。
// 对于本地投影而言,这种近似足够接近,
// 可以得到一个有意义的(即使不是 100% 精确的)比例尺控件。

const projection = new Projection({
  code: 'EPSG:21781',
  units: 'm',
});

const map = new Map({
  controls: defaultControls().extend([new ScaleLine()]),
  layers: layers,
  target: 'map',
  view: new View({
    center: [660000, 190000],
    projection: projection,
    zoom: 9,
  }),
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>无投影的 WMS</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "wms-no-proj",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}