预加载瓦片
预加载瓦片示例。 预加载 OSM
上方的地图会预加载低分辨率的瓦片,下方的地图不使用任何预加载。尝试缩小并平移地图,观察两者的区别。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';
const view = new View({
center: [-4808600, -2620936],
zoom: 8,
});
const map1 = new Map({
layers: [
new TileLayer({
preload: Infinity,
source: new OSM({}),
}),
],
target: 'map1',
view: view,
});
const map2 = new Map({
layers: [
new TileLayer({
preload: 0, // 默认值
source: new OSM({}),
}),
],
target: 'map2',
view: view,
});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="map1" class="map"></div>
<div id="map2" class="map"></div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "preload",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}