read and set features in custom tileLoadFunction

This commit is contained in:
mike-000
2020-04-17 00:01:28 +01:00
committed by GitHub
parent 7d2e367fd0
commit 71b866947f
+10 -12
View File
@@ -72,24 +72,20 @@ fetch(url)
extent: 4096, extent: 4096,
debug: 1, debug: 1,
}); });
const vectorSource = new VectorTileSource({ const format = new GeoJSON({
format: new GeoJSON({
// Data returned from geojson-vt is in tile pixel units // Data returned from geojson-vt is in tile pixel units
dataProjection: new Projection({ dataProjection: new Projection({
code: 'TILE_PIXELS', code: 'TILE_PIXELS',
units: 'tile-pixels', units: 'tile-pixels',
extent: [0, 0, 4096, 4096], extent: [0, 0, 4096, 4096],
}), }),
}), });
const vectorSource = new VectorTileSource({
tileUrlFunction: function (tileCoord) { tileUrlFunction: function (tileCoord) {
// Use the tile coordinate as a pseudo URL for caching purposes // Use the tile coordinate as a pseudo URL for caching purposes
return JSON.stringify(tileCoord); return JSON.stringify(tileCoord);
}, },
}); tileLoadFunction: function (tile, url) {
// For tile loading obtain the GeoJSON for the tile coordinate
// before calling the default tileLoadFunction
const defaultTileLoadFunction = vectorSource.getTileLoadFunction();
vectorSource.setTileLoadFunction(function (tile, url) {
const tileCoord = JSON.parse(url); const tileCoord = JSON.parse(url);
const data = tileIndex.getTile(tileCoord[0], tileCoord[1], tileCoord[2]); const data = tileIndex.getTile(tileCoord[0], tileCoord[1], tileCoord[2]);
const geojson = JSON.stringify( const geojson = JSON.stringify(
@@ -99,10 +95,12 @@ fetch(url)
}, },
replacer replacer
); );
defaultTileLoadFunction( const features = format.readFeatures(geojson, {
tile, extent: vectorSource.getTileGrid().getTileCoordExtent(tileCoord),
'data:application/json;charset=UTF-8,' + geojson featureProjection: map.getView().getProjection()
); });
tile.setFeatures(features);
},
}); });
const vectorLayer = new VectorTileLayer({ const vectorLayer = new VectorTileLayer({
source: vectorSource, source: vectorSource,