javascript - 无法使用 openlayers 显示 pbf(mapbox 矢量切片)

标签 javascript openlayers mapbox vector-tiles osm.pbf

我正在尝试显示我自己创建的矢量图 block (.pbf),它们在检查元素的控制台上没有显示且没有错误。 我通过使用一个简单的示例 html 文件来显示它们: here

我只是更改了在本地服务器而不是 Mapbox 服务器上查找矢量切片的 URL。

当我尝试显示来自 Mapbox 服务器的 pbf 文件时: ,它就会显示出来。我还下载了这些 Mapbox pbf 文件,并从本地服务器提供相同的文件,它们显示出来。

但是我使用tippecanoe从geojson文件创建的pbf文件无法通过openlayers显示。生成的pbf文件是正确的,因为我能够使用mapbox js脚本显示它们。 我打开了cors,服务器的内容编码也是gzip。 示例 pbf 文件不是通过附加代码显示的,而是通过tileserver-gl(mapbox脚本)显示的,可以从以下地址下载 here

代码:

<!DOCTYPE html>
<html>
<head>
    <title>Mapbox Vector Tiles</title>
    <link rel="stylesheet"href="https://openlayers.org/en/v4.5.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.5.0/build/ol.js"></script>
    <script src="https://openlayers.org/en/v4.5.0/examples/resources/mapbox-streets-v6-style.js"></script>
    <script src='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.js'></script>
    <style>
        .map {
            background: #f8f4f0;
        }
    </style>
</head>
<body>
<div id="map" class="map"></div>
<script>
    var key = 'pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiRk1kMWZaSSJ9.E5BkluenyWQMsBLsuByrmg';

    var map = new ol.Map({
        layers: [
            new ol.layer.VectorTile({
                declutter: true,
                source: new ol.source.VectorTile({
                    attributions: '© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' +
                    '© <a href="https://www.openstreetmap.org/copyright">' +
                    'OpenStreetMap contributors</a>',
                    format: new ol.format.MVT(),

                    url: 'http://localhost:8000/tippecanoe_tiles/' + '{z}/{x}/{y}.pbf'
                    //url: 'https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' +
                    //          '{z}/{x}/{y}.vector.pbf?access_token=' + key

                }),
                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],
            zoom: 2
        })
    });
</script>
</body>
</html>

最佳答案

您使用的是 OpenLayers 4.5 而不是 OpenLayers 5.X,请尝试上传您的 OpenLayers 版本。

关于javascript - 无法使用 openlayers 显示 pbf(mapbox 矢量切片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47982534/

相关文章:

php - Facebook 实时更新

javascript - 如何使用 JavaScript 函数(其值由 XSLT 提供)检查条件后突出显示表格单元格

javascript - 是否可以在 OpenLayers 中更改弹出窗口的 HTML?

openlayers - 当标记移动时,开放层删除通过的线

javascript - 循环遍历 Mapbox 中的坐标

javascript - Mapbox 空白 map React-map-gl | react JS

javascript - React 服务器和 React 客户端之间的区别

javascript - DOM API 是 JavaScript 语言的一部分吗?

javascript - 获取 OpenLayers GET 请求的 responseText JSON 元素

mapbox - 如何在 mapbox 中查看最新的开放街道 map