Skip to content

Mapbox Vector 图层 ​

Mapbox Vector 图层示例。 Mapbox studio 矢量 瓦片

ol-mapbox-style 包中的 MapboxVector 图层允许你基于 Mapbox 托管的样式,使用单一矢量数据源创建图层。如果样式使用了多个数据源,可以通过 source 属性选择其中一个矢量数据源。如果只想渲染样式图层的一个子集(前提是它们共享同一个数据源),可以使用 layers 属性。

main.js
js
import {MapboxVectorLayer} from 'ol-mapbox-style';
import Map from 'ol/Map.js';
import View from 'ol/View.js';

const map = new Map({
  target: 'map',
  layers: [
    new MapboxVectorLayer({
      styleUrl: 'mapbox://styles/mapbox/bright-v9',
      accessToken: 'Your Mapbox access token from https://mapbox.com/ here',
    }),
  ],
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Mapbox Vector 图层</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": "mapbox-vector-layer",
  "dependencies": {
    "ol": "10.11.0",
    "ol-mapbox-style": "^13.4.1"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}