矢量瓦片 reprojected
矢量瓦片 reprojected示例。 vectortiles 瓦片 Mapbox maplibre ol-mapbox-style 重投影
矢量瓦片图层可以被重投影到其他坐标系。本示例展示了将地理坐标(EPSG:4326)下的矢量瓦片重投影到北极方位角等积投影(ESRI:102017)。对于极地地区,推荐使用地理坐标系下的矢量瓦片,因为 Web Mercator 瓦片在北纬和南纬 85° 以外无法正常工作。
注意:使用本示例时,请确保在 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 {fromLonLat} from 'ol/proj.js';
import {register} from 'ol/proj/proj4.js';
import VectorTileSource from 'ol/source/VectorTile.js';
import {createXYZ} from 'ol/tilegrid.js';
import proj4 from 'proj4';
// 注册北极兰伯特方位角等积投影的 proj4 定义
proj4.defs(
'ESRI:102017',
'+proj=laea +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs',
);
register(proj4);
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 layer = new VectorTileLayer({
declutter: true,
source: new VectorTileSource({
projection: 'EPSG:4326',
// 与服务器的分辨率相匹配
tileGrid: createXYZ({
extent: [-180, -90, 180, 90],
tileSize: 512,
maxResolution: 180 / 512,
maxZoom: 13,
}),
}),
});
applyStyle(layer, url);
applyBackground(layer, url);
const map = new Map({
target: 'map',
layers: [layer],
view: new View({
projection: 'ESRI:102017',
minZoom: 3,
zoom: 4,
center: fromLonLat([0, 90], 'ESRI:102017'),
}),
});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" style="background:none;"></div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "vector-tiles-reprojected",
"dependencies": {
"ol": "10.11.0",
"ol-mapbox-style": "^13.4.1",
"proj4": "2.20.9"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}