我想在 OpenLayers 中包含一个矢量平铺地图,并使用 GL 样式文件来设置地图的样式。因此,我使用的是 ol mapbox 样式(olms)。我包括了 ol 版本 6.4.3 和 olms 版本 6.1.3。我想在 ID 为“map”的 DOM 对象中渲染我的地图。我从 OpenMapTiles 服务器获取我的图块,我自己托管(在 localhost:32768 上)。这是我的代码:
const vectorTileLayer = new ol.layer.VectorTile({
source: new ol.source.VectorTile({
attributions: [
'<a href="http://www.openmaptiles.org/" target="_blank">© OpenMapTiles</a>',
'<a href="http://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap contributors</a>',
],
format: new ol.format.MVT(),
url: 'http://localhost:32768/data/v3/{z}/{x}/{y}.pbf',
maxZoom: 18,
}),
});
this.map = new ol.Map({
target: 'map',
loadTilesWhileAnimating: true,
loadTilesWhileInteracting: true,
view: new ol.View(this.viewConfig),
});
fetch('http://localhost:32768/styles/osm-bright/style.json').then(function(response) {
response.json().then(function(glStyle) {
olms.applyStyle(vectorTileLayer, glStyle, 'openmaptiles').then(function() {
me.map.addLayer(vectorTileLayer);
});
});
});
我尝试了不同来源的不同实现(使用 stylefunction() 函数,使用 apply() 函数)并消除了所有其他可能导致错误的因素。
如果我用地图加载页面,我会收到以下错误:
TextBuilder.js:502 Uncaught TypeError: t.getScaleArray is not a function
at e.setTextStyle (TextBuilder.js:502)
at Point (vector.js:239)
at vector.js:123
at Fo (vector.js:102)
at e.renderFeature (VectorTileLayer.js:565)
at e.x (VectorTileLayer.js:258)
at d (VectorTileLayer.js:271)
at e.updateExecutorGroup_ (VectorTileLayer.js:286)
at e.prepareTile (VectorTileLayer.js:131)
at e.dispatchEvent (Target.js:114)
当不从我的 Tile Server 获取样式(并使用 olms 应用它们)并在创建地图后立即添加图层时,我没有收到错误,但当然我也没有样式。
我还包括了这个样式文件中需要的字体。