CartoDB / torque

Temporal mapping for CARTO
http://cartodb.github.com/torque/
BSD 4-Clause "Original" or "Old" License
397 stars 129 forks source link

ol.TileLoader.prototype._initTileLoader does not work with newer versions of OL #303

Open senakafdo opened 5 years ago

senakafdo commented 5 years ago

I have checked this code with OL 3.17.1 and it does work well. However, it does not work with OL 5.3.0. There are two issues.

  1. The way this is used in the code is not accurate, as it could be different within an event handler and in OL 5.3.0, they change it to something else. A better way would be to store a reference to this and use that instead.
  2. There is no longer a method called unByKey(). But, they support un() which works in OL 3.17.1 and also 5.3.0. The improvement below fixes the code such that it works on both OL 3.17.1 and 5.3.0.
ol.TileLoader.prototype._initTileLoader = function (map) {
    const _that = this;
    this._map = map;
    this._view = map.getView();
    this._centerChangedId = this._view.on('change:center', function (e) {
        _that._updateTiles();
    }, this);

    this._postcomposeKey = undefined;

    this._resolutionChangedId = this._view.on('change:resolution', function (evt) {
        _that._currentResolution = _that._view.getResolution();
        if (_that._postcomposeKey) return;
        _that.fire('mapZoomStart');
        _that._postcomposeKey = _that._map.on('postcompose', function (evt) {
            if (evt.frameState.viewState.resolution === _that._currentResolution) {
                _that._updateTiles();
                _that._map.un('postcompose', _that._postcomposeKey, _that);
                _that._postcomposeKey = undefined;
                _that.fire('mapZoomEnd');
            }
        }, _that);
    }, this);

    this._updateTiles();
};

I have however not tested this for versions in between these two. There is also widespread use of unByKey() in the code, which could be replaced with un().