如何在传单中显示由geojson-vt生成的矢量切片?

Røy*_*øye 10 javascript geojson leaflet vector-tiles

我想要在传单地图上显示很多GeoJSON空间数据.大约35,000个GeoJSON对象.

因为点的数量可能会非常大,我想使用geojson-vt库在客户端平铺我的数据.

现在我已经使用geojson-vt库成功平铺了我的数据:

var geoJson = {}; // Request to get data via API call not shown here

var tileOptions = {
        maxZoom: 18, 
        tolerance: 5, 
        extent: 4096,
        buffer: 64, 
        debug: 0, 
        indexMaxZoom: 0, 
        indexMaxPoints: 100000, 
    };

var tileIndex = geojsonvt(geoJson, tileOptions);
Run Code Online (Sandbox Code Playgroud)

如何将geojson-vt生成的矢量切片数据集成到我的Leaflet地图中?

是否有任何推荐的插件或库可以提供帮助?

Ste*_*ett 4

Geojson-vtTileIndex.getTile()返回 Mapbox 矢量切片规范的 JSON 版本:

在此输入图像描述

我不知道有任何库可以显示这种格式。事实上,Mapbox 自己的演示在相当低的级别上实现了可视化:

var tile = tileIndex.getTile(z, x, y);

console.timeEnd('getting tile z' + z + '-' + x + '-' + y);

if (!tile) {
    console.log('tile empty');
    zoomOut();
    return;
}

// console.log('z%d-%d-%d: %d points of %d', z, x, y, tile.numSimplified, tile.numPoints);
// console.time('draw');

ctx.clearRect(0, 0, height, height);

var features = tile.features;

ctx.strokeStyle = 'red';
ctx.fillStyle = 'rgba(255,0,0,0.05)';

for (var i = 0; i < features.length; i++) {
    var feature = features[i],
        type = feature.type;

    ctx.beginPath();

    for (var j = 0; j < feature.geometry.length; j++) {
        var geom = feature.geometry[j];

        if (type === 1) {
            ctx.arc(geom[0] * ratio + pad, geom[1] * ratio + pad, 2, 0, 2 * Math.PI, false);
            continue;
        }

        for (var k = 0; k < geom.length; k++) {
            var p = geom[k];
            if (k) ctx.lineTo(p[0] * ratio + pad, p[1] * ratio + pad);
            else ctx.moveTo(p[0] * ratio + pad, p[1] * ratio + pad);
        }
    }

    if (type === 3 || type === 1) ctx.fill('evenodd');
    ctx.stroke();
}
drawGrid();
Run Code Online (Sandbox Code Playgroud)

您也许可以使用他们的一些代码来帮助您。

自述文件和相关博客文章中有各种引用提到 Mapbox-gl-js 由 geojson-vt“提供支持”,但没有明确说明如何实现这一点。也许更好的方法是简单地使用mapbox-gl-js GeoJSONSource