Skip to content

Polygon map wrapping ​

Polygon map wrapping示例。 简单 geometry rectangle box bbox 多边形 dateline wrap 180

一个简单的地图,底图为 OSM,并在 antimeridian(反经线)两侧(以及跨越反经线的位置)放置了几个矩形多边形要素(例如边界框)。

main.js
js
import Feature from 'ol/Feature.js';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {fromExtent} from 'ol/geom/Polygon.js';
import TileLayer from 'ol/layer/Tile.js';
import VectorLayer from 'ol/layer/Vector.js';
import {get as getProjection} from 'ol/proj.js';
import OSM from 'ol/source/OSM.js';
import VectorSource from 'ol/source/Vector.js';

const proj = getProjection('EPSG:3857');
const extent = proj.getExtent();
const leftX = extent[0];
const rightX = extent[2];

const map = new Map({
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
    new VectorLayer({
      updateWhileAnimating: true,
      updateWhileInteracting: true,
      source: new VectorSource({
        features: [
          new Feature(fromExtent([leftX, -15000000, leftX, 15000000])),
          new Feature(
            fromExtent([leftX - 2000000, 4500000, leftX + 4000000, 6500000]),
          ),
          new Feature(
            fromExtent([rightX - 4000000, 7500000, rightX + 2000000, 9500000]),
          ),
        ],
      }),
    }),
  ],
  target: 'map',
  view: new View({
    center: [0, 6000000],
    zoom: 1,
  }),
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>多边形地图环绕</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": "polygon-wrapping",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}