我有一张图像加载到我的地图上以指示用户的 GPS 位置。我没有使用 Mapbox 的内置 GPS 控件,而是从外部获取数据并使用图像层和源显示它。它只是一个蓝色箭头。
当 javascript 执行时,它似乎按预期显示没有问题。但是,我收到一个控制台错误,我担心在某些时候会出现问题(如果它还没有出现并且我还没有注意到它。)
首先,我的控制台错误:
util.js:458 Image "arrow" could not be loaded. Please make sure you have added the image with map.addImage() or a "sprite" property in your style. You can provide missing images by listening for the "styleimagemissing" map event.
这是我用来加载图像、图层和源的代码:
map.loadImage('images/gps/arrow.png', function(error, image) {
if (error)
throw error;
map.addImage('arrow', image);
});
map.addSource('gps_point', {
type: 'geojson',
data: window.userLatestPoint
});
map.addLayer({
"id": "gps_point",
"type": "symbol",
"source": "gps_point",
"layout": {
"icon-image": "arrow",
"icon-allow-overlap": true,
"text-allow-overlap": true,
"icon-rotate": ["get", "rotate"]
}
});
我不知道为什么这会引发错误,除非我做错了什么但 Mapbox 无论如何都允许它。
loadImage
是一个异步操作,所以你应该调用增加addSource
和addLayer
功能的范围之内loadImage
。
否则很可能图层在加载图像之前开始绘制并引发这些错误,但是由于图层定义中存在的引用,当它最终加载时会被查看。
您可以在官方文档中的Mapbox 示例中看到这种良好做法
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句