Advanced Mapbox Vector Tiles-進階Mapbox矢量貼圖
知識要點
- (主要)使用此方法加載矢量地圖可以在縮放級别時重用相同的源瓦片,一定程度上節省移動裝置的帶寬,網絡環境不好時,可以達到提升地圖加載速度的效果。
- (主要)調用mapbox地圖服務需要在mapbox官網新增賬號,在控制台擷取APP的指定Access token令牌,這是能調取矢量地圖的關鍵。
- (次要)openlayers官方執行個體為我們引入了mapbox地圖樣式V6版本,具體可檢視源碼中,/examples/resources/mapbox-streets-v6-style.js檔案。
官網原文及譯文
A vector tiles map which reuses the same source tiles for subsequent zoom levels to save bandwidth on mobile devices. Note: No map will be visible when the access token has expired.
矢量瓦片地圖,在後續縮放級别時重用相同的源瓦片,以節省移動裝置上的帶寬。注意:當access token 通路令牌過期時,地圖将不顯示。
源碼
<!DOCTYPE html>
<html lang="zn">
<head>
<meta charset="UTF-8">
<!-- 引入OpenLayers CSS樣式 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/[email protected]/en/v6.13.0/css/ol.css"
type="text/css">
<!-- 引入OpenLayers JS庫 -->
<script src="https://cdn.jsdelivr.net/gh/openlayers/[email protected]/en/v6.13.0/build/ol.js"></script>
<!-- 引入mapbox地圖樣式V6版本 -->
<script src="https://openlayers.org/en/v6.13.0/examples/resources/mapbox-streets-v6-style.js"></script>
<!-- css代碼 -->
<style>
.map {
width: 100%;
height: 400px;
background: #f8f4f0;
}
</style>
<title>Advanced Mapbox Vector Tiles-進階Mapbox矢量貼圖</title>
</head>
<body>
<!-- html代碼 -->
<div id="map" class="map"></div>
<!-- script代碼 -->
<script>
// 聲明key變量,key的擷取途徑,進入mapbox官網 -> 新增賬號 -> 建立APP -> 擷取Access token,mapbox官網位址:https://www.mapbox.com
const key =
'pk.eyJ1Ijoiemhhb3lhaHVpIiwiYSI6ImNsMDVhM2xmcjF3MWgzZXFoMXExb3luczYifQ.BL1X1DNYDd6UrlfEoCrGNA';
// 計算比對縮放級别1、3、5、7、9、11、13、15的分辨率
const resolutions = [];
for (let i = 0; i <= 8; ++i) {
resolutions.push(156543.03392804097 / Math.pow(2, i * 2));
}
// 計算縮放級别1、3、5、7、9、11、13、15的tile url
function tileUrlFunction(tileCoord) {
return (
'https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' +
'{z}/{x}/{y}.vector.pbf?access_token=' +
key
)
.replace('{z}', String(tileCoord[0] * 2 - 1))
.replace('{x}', String(tileCoord[1]))
.replace('{y}', String(tileCoord[2]))
.replace(
'{a-d}',
'abcd'.substr(((tileCoord[1] << tileCoord[0]) + tileCoord[2]) % 4, 1)
);
}
// 初始化地圖
const map = new ol.Map({
// 設定圖層
layers: [
new ol.layer.VectorTile({
source: new ol.source.VectorTile({
// 設定歸屬标簽
attributions:
'© <a href="https://www.mapbox.com/map-feedback/" target="_blank" rel="external nofollow" >Mapbox</a> ' +
'© <a href="https://www.openstreetmap.org/copyright" target="_blank" rel="external nofollow" >' +
'OpenStreetMap contributors</a>',
// 瓦片的特征格式化,此處使用MVT表示mapbox vector tile(mapbox矢量瓦片)
format: new ol.format.MVT(),
// 瓦片網格
tileGrid: new ol.tilegrid.TileGrid({
// 瓦片網格的範圍
extent: ol.proj.get('EPSG:3857').getExtent(),
// 分辨率級别數組
resolutions: resolutions,
// 瓦片尺寸
tileSize: 512,
}),
// 獲得給定的tile坐标和投影的tile URL
tileUrlFunction: tileUrlFunction,
}),
// 調createMapboxStreetsV6Style函數, 應用mapbox專用樣式
style: createMapboxStreetsV6Style(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text),
}),
],
// 綁定頁面元素
target: 'map',
// 設定視圖
view: new ol.View({
center: [0, 0],
minZoom: 1,
zoom: 2,
}),
});
</script>
</body>
</html>
效果截圖
