map.addSource(sourceId, {
type: 'geojson',
data: 'http://path-to-source,
});
在上传数据时,有什么方法可以在不从RAW GEOJSON中计算边界框?我已经尝试使用getlayer和getSource,但范围不在那里。
同样,如果有多层切换,我想将所有图层的界限放在一起。
我遇到了相同的问题,该问题也已在(封闭的)Githubessueph.
中提出。
i在其他螺纹上找到了一个潜在的解决方案,该解决方案依赖于querySourceFeatures
最终,我进行了解决方法,因为缺少使用Maplibre GL JS更好的替代方案:而不是将源添加为远程Geojson文件,而是提前获取并直接使用Geojson对象。
// Load GeoJSON
const response = await fetch("geojson.json");
const geojson = await response.json();
// Fit bounds
const bounds = new maplibregl.LngLatBounds();
geojson.features.forEach(function (feature) {
bounds.extend(feature.geometry.coordinates);
});
map.fitBounds(bounds, { padding: 100, animate: false });
// Add map source
map.addSource("source_id", {
type: 'geojson',
data: geojson,
});
我希望有一种直接使用Maplibre直接进行此操作的方法,但看起来并不是很快就会到来。至少手动获取JSON可确保我们不仅依赖浏览器缓存。