Skip to content

矢量瓦片 in EPSG:4326 ​

矢量瓦片 in EPSG:4326示例。 vectortiles EPSG:4326 mapboxstyle ol-mapbox-style MapTiler 网格

展示从 maptiler.com 加载的 EPSG:4326 矢量瓦片(使用 ol-mapbox-style 进行样式设置)的示例。注意:使用本示例时,请确保在 https://www.maptiler.com/cloud/ 获取你自己的 API 密钥。当 API 密钥过期时,地图将无法显示。

main.js
js
import {applyBackground, applyStyle} from 'ol-mapbox-style';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import VectorTileLayer from 'ol/layer/VectorTile.js';
import VectorTileSource from 'ol/source/VectorTile.js';
import {createXYZ} from 'ol/tilegrid.js';

const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const url = 'https://api.maptiler.com/maps/basic-4326/style.json?key=' + key;

// 与服务器的分辨率相匹配
const tileGrid = createXYZ({
  extent: [-180, -90, 180, 90],
  tileSize: 512,
  maxResolution: 180 / 512,
  maxZoom: 13,
});

const layer = new VectorTileLayer({
  declutter: true,
  source: new VectorTileSource({
    projection: 'EPSG:4326',
    tileGrid: tileGrid,
  }),
});
applyStyle(layer, url, {resolutions: tileGrid.getResolutions()});
applyBackground(layer, url);

const map = new Map({
  target: 'map',
  layers: [layer],
  view: new View({
    projection: 'EPSG:4326',
    zoom: 0,
    center: [0, 30],
  }),
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>EPSG:4326 矢量瓦片</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map" style="background:none;"></div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "vector-tiles-4326",
  "dependencies": {
    "ol": "10.11.0",
    "ol-mapbox-style": "^13.4.1"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}