Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion packages/maptalks/src/layer/tile/TileLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ const options: TileLayerOptionsType = {
'depthMask': true,
'currentTilesFirst': true,
'forceRenderOnMoving': true,
'tileErrorScale': 1
'tileErrorScale': 1,
'detectRetina': false
};

const URL_PATTERN = /\{ *([\w_]+) *\}/g;
Expand All @@ -185,6 +186,28 @@ const TILE_MIN = [0, 0, 0];
const TILE_MAX = [0, 0, 0];
const ARR3: Vector3 = [0, 0, 0];

const RetinaScales = [0.25, 0.5, 1, 2, 4];

function getRetinaScale(dpr: number, reverse?: boolean) {
if (reverse) {
for (let i = RetinaScales.length - 1; i >= 0; i--) {
const retinaScale = RetinaScales[i];
if (dpr >= retinaScale) {
return retinaScale
}
}
} else {
for (let i = 0, len = RetinaScales.length; i < len; i++) {
const retinaScale = RetinaScales[i];
if (dpr <= retinaScale) {
return retinaScale
}
}
}

return null;
}

/**
* A layer used to display tiled map services, such as [google maps](http://maps.google.com), [open street maps](http://www.osm.org)
* @category layer
Expand Down Expand Up @@ -229,6 +252,8 @@ class TileLayer extends Layer {
//record spatial reference in current rendering frame
//@internal
_spatialRef: SpatialReference;
//@internal
_zoomOffset: number;
options: TileLayerOptionsType;

/**
Expand All @@ -238,6 +263,8 @@ class TileLayer extends Layer {
*/
constructor(id: string, options?: TileLayerOptionsType) {
super(id, options);
//record original zoomOffset
this._zoomOffset = this.options['zoomOffset'] || 0;
}

/**
Expand Down Expand Up @@ -286,6 +313,27 @@ class TileLayer extends Layer {
size = [size, size];
}
this._tileSize = new Size(size);
if (!this.options.detectRetina) {
return this._tileSize;
}
const map = this.getMap();
if (map) {
const dpr = map.getDevicePixelRatio();
if (Math.abs(dpr - 1) > 0) {
const scale = getRetinaScale(dpr, dpr < 1);
if (scale && scale !== 1) {
const [width, height] = size;
const w = Math.floor(width / scale), h = Math.floor(height / scale);
let offset = Math.max(width, w) / Math.min(width, w) / 2;
if (dpr < 1) {
offset = -offset;

}
this.options.zoomOffset = this._zoomOffset + offset;
this._tileSize = new Size(w, h);
}
}
}
return this._tileSize;
}

Expand Down Expand Up @@ -1752,6 +1800,7 @@ export type TileLayerOptionsType = LayerOptionsType & {
mipmapTexture?: boolean;
currentTilesFirst?: boolean;
tileErrorScale?: number;
detectRetina?: boolean;
};

enum TileVisibility {
Expand Down
22 changes: 13 additions & 9 deletions packages/vt/src/layer/layer/VectorTileLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ const defaultOptions: VectorTileLayerOptionsType = {
tileStackDepth: 2,

altitudePropertyName: null,
disableAltitudeWarning: false
disableAltitudeWarning: false,
loadTileErrorLog: true,
loadTileErrorLogIgnoreCodes: [404, 204]
};

/**
Expand Down Expand Up @@ -347,12 +349,12 @@ class VectorTileLayer extends maptalks.TileLayer {
}

forceReload(): this {
// expire cached tiles in worker
const renderer = this.getRenderer() as any;
if (renderer) {
renderer._incrWorkerCacheIndex();
}
return super.forceReload();
// expire cached tiles in worker
const renderer = this.getRenderer() as any;
if (renderer) {
renderer._incrWorkerCacheIndex();
}
return super.forceReload();
}

onWorkerReady() { }
Expand Down Expand Up @@ -1832,7 +1834,7 @@ class VectorTileLayer extends maptalks.TileLayer {
clear() {
const renderer = this.getRenderer();
if (renderer) {
renderer.clearData();
renderer.clearData();
}
return super.clear();
}
Expand Down Expand Up @@ -2011,7 +2013,9 @@ export type VectorTileLayerOptionsType = {
style?: any,

altitudePropertyName?: string,
disableAltitudeWarning?: boolean
disableAltitudeWarning?: boolean,
loadTileErrorLog?: boolean,
loadTileErrorLogIgnoreCodes?: Array<number>;
} & TileLayerOptionsType;

export type AsyncFeatureQueryOptions = {
Expand Down
4 changes: 4 additions & 0 deletions packages/vt/src/layer/renderer/VectorTileLayerRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ class VectorTileLayerRenderer extends CanvasCompatible(TileLayerRendererable(Lay
const referrer = window && window.location.href;
const altitudePropertyName = this.layer.options['altitudePropertyName'];
const disableAltitudeWarning = this.layer.options['disableAltitudeWarning'];
const loadTileErrorLog = this.layer.options.loadTileErrorLog;
const loadTileErrorLogIgnoreCodes = this.layer.options.loadTileErrorLogIgnoreCodes;
const loadTileOpitons = {
tileInfo: {
res: tileInfo.res,
Expand All @@ -549,6 +551,8 @@ class VectorTileLayerRenderer extends CanvasCompatible(TileLayerRendererable(Lay
},
glScale,
disableAltitudeWarning,
loadTileErrorLog,
loadTileErrorLogIgnoreCodes,
altitudePropertyName,
zScale: this._zScale,
centimeterToPoint,
Expand Down
15 changes: 12 additions & 3 deletions packages/vt/src/worker/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNumber } from '../common/Util';
import Dispatcher from './Dispatcher';

export const initialize = function () {
Expand All @@ -15,11 +16,19 @@ export const onmessage = function (message, postResponse) {
}
} else {
const command = data.command;
const loadTileErrorLog = (data.params || {}).loadTileErrorLog;
const loadTileErrorLogIgnoreCodes = (data.params || {}).loadTileErrorLogIgnoreCodes || [];
this.dispatcher[command]({ actorId: message.actorId, mapId: data.mapId, layerId: data.layerId, params: data.params }, (err, data, buffers) => {
if (err && err.status !== 404 && err.status !== 204 && !err.loading) {
// err.loading 为true时,说明geojson-vt正在创建索引
console.error(command, err);
if (loadTileErrorLog && err && !err.loading) {
const status = err.status;
if (isNumber(status) && loadTileErrorLogIgnoreCodes.indexOf(status) === -1) {
console.error(command, err);
}
}
// if (err && err.status !== 404 && err.status !== 204 && !err.loading) {
// // err.loading 为true时,说明geojson-vt正在创建索引
// console.error(command, err);
// }
postResponse(err, data, buffers);
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/vt/src/worker/layer/VectorTileLayerWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class VectorTileLayerWorker extends LayerWorker {
}, 1);
}
fetchOptions.referrer = context.referrer;
fetchOptions.errorLog = context.loadTileErrorLog;
return Ajax.getArrayBuffer(url, fetchOptions, (err, response) => {
if (!this._cache) {
// removed
Expand Down
18 changes: 13 additions & 5 deletions packages/vt/src/worker/util/Ajax.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isFunction, uid } from '../../common/Util';
import { isFunction, isNil, uid } from '../../common/Util';

const USE_FETCH = typeof fetch === 'function' && typeof AbortController === 'function';
const USE_FETCH = typeof fetch === 'function' && typeof AbortController === 'function';

/**
* @classdesc
Expand Down Expand Up @@ -68,6 +68,10 @@ const Ajax = {
options = t;
}
options = options || {};
let errorLog = options.errorLog;
if (isNil(errorLog)) {
errorLog = true;
}
if (options.method) {
options.method = options.method.toUpperCase();
}
Expand Down Expand Up @@ -115,14 +119,18 @@ const Ajax = {
}
}).catch(err => {
if (!err.code || err.code !== DOMException.ABORT_ERR) {
console.error('Fetch error:', url, err);
if (errorLog) {
console.error('Fetch error:', url, err);
}
cb(err);
}
});
}
}).catch(err => {
if (!err.code || err.code !== DOMException.ABORT_ERR) {
console.error('Fetch error:', url, err);
if (errorLog) {
console.error('Fetch error:', url, err);
}
cb(err);
}
});
Expand Down Expand Up @@ -185,7 +193,7 @@ const Ajax = {
client = new XMLHttpRequest();
} catch (e) {
try { client = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {
try { client = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
try { client = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { }
}
}
client.onreadystatechange = Ajax._wrapCallback(client, cb);
Expand Down