【问题标题】:Cannot add BingMaps layer dynamically无法动态添加 BingMaps 图层
【发布时间】:2019-04-27 20:29:54
【问题描述】:

我正在尝试将 BingMaps 图层添加到已初始化的地图中。我遇到了一个我无法理解的错误。你知道出了什么问题吗?我正在使用 OpenLayers 5.3.1。

TypeError: r is null[Weitere Informationen] map.js:1:677385
    •   a http://localhost:8080/map/map.js:1 
    •   inverse http://localhost:8080/map/map.js:1 
    •   I http://localhost:8080/map/map.js:1 
    •   transformInv_ http://localhost:8080/map/map.js:1 
    •   u http://localhost:8080/map/map.js:1 
    •   t http://localhost:8080/map/map.js:1 
    •   getTile http://localhost:8080/map/map.js:1 
    •   manageTilePyramid http://localhost:8080/map/map.js:1 
    •   prepareFrame http://localhost:8080/map/map.js:1 
    •   renderFrame http://localhost:8080/map/map.js:1 
    •   renderFrame_ http://localhost:8080/map/map.js:1 
    •   animationDelay_ http://localhost:8080/map/map.js:1 
    •   <anonym> self-hosted:974 

我的打字稿是这样的:

import Map from 'ol/Map';
import OlTileLayer from 'ol/layer/Tile';
import BingMaps from 'ol/source/BingMaps';

@injectable()
export class MapWrapper {
    private _map: Map;

    public getMap() {
        return this._map;
    }

    set map(value: Map) {
        this._map = value;
    }

    public createBingLayer(bingKey: string, style : string) : OlTileLayer {
        return new OlTileLayer({
            visible: true,
            preload: Infinity,
            source: new BingMaps({
                key: bingKey,
                imagerySet: style
            })
        });
    }
}

而我的 HTML 中的 javascript 就是这样的:

    function addBingMap() {
      var realMap = map.mapWrapper.getMap();
      var bingLayer = map.mapHolder.createBingLayer("someAPIKey", "Road");
      //realMap.getLayers().insertAt(0, bingLayer);
      realMap.addLayer(bingLayer);
    }

更新

我发现我看到的错误是由 BingMaps 瓷砖被重新投影到 EPSG:32632 引起的,我的所有其他图层都使用它。 proj4 transformer() 方法中抛出了错误。我为 ol 创建了一个 bug 票,因为我认为它至少应该抛出一个有意义的错误消息,即使我不能使用另一个投影将 BingMaps(网络墨卡托投影)与图层混合。

【问题讨论】:

  • 能否提供一些相关代码?

标签: webpack openlayers


【解决方案1】:

我无法重现该错误,但由于 Bing 源在 API 密钥准备好使用之前对其进行了异步身份验证,因此您可以尝试

function addBingMap() {
  var realMap = map.mapWrapper.getMap();
  var bingLayer = map.mapHolder.createBingLayer("someAPIKey", "Road");
  var onKey = bingLayer.getSource().on("change", function() {
    if (bingLayer.getSource().getState() == "ready") {
      ol.Observable.unByKey(onKey);
      realMap.addLayer(bingLayer);
    }
  });
} 

如果不可能的转换(例如极点到网络墨卡托)导致可以忽略的错误:

    var forward = ol.proj.getTransform(projection1, projection2);
    var inverse = ol.proj.getTransform(projection2, projection1);

    ol.proj.addCoordinateTransforms(
        projection1,
        projection2,
        function(coordinate) {
            try {
                return forward(coordinate)
            } catch (e) {
                return [undefined, undefined];
            }
        },
        function(coordinate) {
            try {
                return inverse(coordinate)
            } catch (e) {
                return [undefined, undefined];
            }
        }
    );

有效的直接转换失败但可以使用中间投影进行工作:

    var forward1 = ol.proj.getTransform(projection1, intermediate);
    var forward2 = ol.proj.getTransform(intermediate, projection2);
    var inverse1 = ol.proj.getTransform(projection2, intermediate);
    var inverse2 = ol.proj.getTransform(intermediate, projection1);

    ol.proj.addCoordinateTransforms(
        projection1,
        projection2,
        function(coordinate) {
            try {
                return forward2(forward1(coordinate));
            } catch (e) {
                return [undefined, undefined];
            }
        },
        function(coordinate) {
            try {
                return inverse2(inverse1(coordinate));
            } catch (e) {
                return [undefined, undefined];
            }
        }
    );

【讨论】:

  • 谢谢,我赞成你的回答,因为这是我看到的另一个问题(虽然只是有时)。但是,我认为这是 ol BingMaps 和图层重投影的问题。
  • 即使在 OpenLayers 自己的示例中也会出现这些错误,例如在 openlayers.org/en/latest/examples/reprojection-by-code.html 上输入 3031 我还向 OL github.com/openlayers/openlayers/issues/9401 提出了问题,因为这是一个 proj4 问题,所以响应是使用不同的转换算法,例如 mproj(我发现它也会产生错误)或根据需要进行自己的错误处理.但是,在将 web 墨卡托瓦片重新投影到基于 WGS84 的 UTM 投影时,我从未遇到过错误。
  • 因为我原来的错误处理函数过于复杂,所以在这个答案中添加了更简单的版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
相关资源
最近更新 更多