protomaps / PMTiles

Cloud-optimized + compressed single-file tile archives for vector and raster maps

Home Page:https://protomaps.com/docs/pmtiles/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OpenLayers PMTilesVectorSource fires tileloadstart event twice per tile

mzmcgrat opened this issue · comments

The OpenLayers implementation of the PMTilesVectorSource class causes the tileloadstart event to be fired two times for each tile that gets loaded. This breaks things that track the tileloadstart and tileloadend events such as loading indicators, etc. You can verify this by seeing the same tile coordinates logged to the console twice in the vector.html example:

<html>
    <head>
        <title>PMTiles OpenLayers Vector Example</title>
        <meta charset="utf-8"/>
        <script src="https://cdn.jsdelivr.net/npm/ol@v7.3.0/dist/ol.js"></script>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v7.3.0/ol.css">
        <script src="https://unpkg.com/ol-pmtiles@0.3.0/dist/olpmtiles.js"></script>
        <style>
            body, #map {
                height:100vh;
                margin: 0;
            }
        </style>
    </head>
    <body>
        <div id="map"></div>
        <script type="text/javascript">
          const source= new olpmtiles.PMTilesVectorSource({
              url: "https://r2-public.protomaps.com/protomaps-sample-datasets/nz-buildings-v3.pmtiles",
              attributions: ["© Land Information New Zealand"]
          })

          //-->> This logs the tile coordinates two times for each tile that gets loaded
          source.on('tileloadstart', function(evt) {
            console.log(evt.tile.tileCoord)
          })

          const vectorLayer = new ol.layer.VectorTile({
            declutter: true,
            source: source,
            style: new ol.style.Style({
              stroke: new ol.style.Stroke({
                color: 'gray',
                width: 1,
              }),
              fill: new ol.style.Fill({
                color: 'rgba(20,20,20,0.9)',
              })
            })
          });

          ol.proj.useGeographic();
          const map = new ol.Map({
            layers: [vectorLayer],
            target: 'map',
            view: new ol.View({
              center: [172.606201,-43.556510],
              zoom: 7
            }),
          });
        </script>
    </body>
</html>

Looking at the example tileLoadFunction in https://openlayers.org/en/latest/apidoc/module-ol_source_VectorTile-VectorTile.html, it appears that you don't need to set tile.setState(TileState.LOADING); in the tile.setLoader function (only need to set the tile state on error). Removing that line from the class appears to fix the issue.

Thanks for the example, should be fixed in 0.4.0