缩放约束
缩放约束示例。 MapTiler 缩放 minZoom maxZoom
该地图使用的视图被限制在第 9 到第 13 缩放级别之间。这是通过视图的 minZoom 和 maxZoom 选项实现的。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import ImageTile from 'ol/source/ImageTile.js';
const key = 'Get your own API key at https://www.maptiler.com/cloud/';
const attributions =
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' +
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>';
const map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new ImageTile({
attributions: attributions,
url:
'https://api.maptiler.com/maps/satellite/{z}/{x}/{y}.jpg?key=' + key,
tileSize: 512,
}),
}),
],
view: new View({
center: [-13553864, 5918250],
zoom: 11,
minZoom: 9,
maxZoom: 13,
}),
});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": "zoom-constrained",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}