OpenLayers addLayers失败,出现TypeError。

问题描述 投票:1回答:1

我正在从OpenLayers 3.15.0更新到6.3.1。当我调用map.addLayer时,我得到以下错误。

Uncaught TypeError: Cannot read property 'ol_uid' of undefined @
ecb://web/java/ol.js:1:23754

这里是调用该函数的上下文。

function getMinZoom() {
  var width = map.clientWidth;
  return Math.ceil(Math.LOG2E * Math.log(width / 256));
}

//create wms layer
function addWMSLayer(url, attTxt, attHref, layer, format, server, res1, res2, res3, res4) {
  initializeMap();
  //tile source for load wms layers
  var newRes1 = Number(getCalcResolutionSrv(res1));
  var newRes2 = Number(getCalcResolutionSrv(res2));
  var newRes3 = Number(getCalcResolutionSrv(res3));
  var newRes4 = Number(getCalcResolutionSrv(res4));

  var newWMSSource = new ol.source.TileWMS({
    url: url,
    params: {
      'LAYERS': layer,
      'FORMAT': format,
    },
    serverType: 'mapserver',
    projection: projection
  });

  var minZoom = getMinZoom();
  var newWmsLayer = new ol.layer.Tile({
    extent: extent,
    source: newWMSSource,
    minResolution: newRes4,
    maxResolution: newRes1,
    zIndex: 0,
    minZoom: minZoom
  });
  wmsResolution.push(Number(newRes1 - 0.00100));
  wmsResolution.push(newRes2);
  wmsResolution.push(newRes3);
  wmsResolution.push(Number(newRes4 + 0.00100));
  map.addLayer(newWmsLayer);
  ECBJS.addNewWMSLayer(url, layer, newRes1, newRes2, newRes3, newRes4);
};

这个函数与3.15.0中调用addLayer的函数完全相同,除了zIndex和minZoom这两个属性。在3.15.0中,它是正常的。

问题可能出在哪里?

更新

我基于CefSharp的MinimalExample创建了一个MinimalExample解决方案。你需要Visual Studio或Rider来打开它。https:/github.comtbremeyerCefSharp.MinimalExample.git。

目前,在MainWindows.xaml.cs的函数CallWebSite中调用EvaluateScriptAsync时,返回的是

Message = "Uncaught TypeError: Cannot read property 'ol_uid' of
undefined @ ecb://web/java/ol.js:1:23754" 
Success = false

我希望它能带着

Message = ""
Success = true
javascript openlayers
1个回答
0
投票

地图对象没有被正确初始化。下面是创建地图的代码。

var map = new ol.Map({
    controls: ol.control.defaults().extend([
        new ol.control.ScaleLine({
            units: 'metric'
        })
    ]).extend([mousePositionControl]),
    layers: [filterVector],
    overlays: [overlay],
    logo: false,
    target: 'map',
    view: new ol.View({
    })
});

它使用了mousePositionControl的扩展。mousePositionControl被定义为。

var mousePositionControl = new ol.control.MousePosition(
    {
        coordinateFormat: ol.coordinate.createStringXY(1),
        projection: 'EPSG:2056',
        className: 'custom-mouse-position',
        target: document.getElementById('info'),
        undefinedHTML: ' '
    });

所以它使用了一个投影 投影没有得到正确的初始化。

var extent = [2420000, 130000, 2900000, 1350000];
var jsLayers = new Object();
if (proj4) {
    proj4.defs("EPSG:2056",   "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs");
};

var projection = ol.proj.get('EPSG:2056');
projection.setExtent(extent);

OpenLayers.Map在使用非 "标准 "的EPSG代码时无法渲染。使用非 "标准 "的EPSG代码时,地图无法呈现。 我知道了,我可以用

ol.proj.proj4.register(proj4);

来帮助投影。现在的内容是:

var extent = [2420000, 130000, 2900000, 1350000];
var jsLayers = new Object();
if (proj4) {
    proj4.defs("EPSG:2056",   "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs");
};

ol.proj.proj4.register(proj4);
var projection = ol.proj.get('EPSG:2056');
projection.setExtent(extent);

修改后,filterVector不见了 覆盖的定义必须移到地图初始化之前。

var filterSource = new ol.source.Vector({ wrapX: false });
var filterVector = new ol.layer.Vector({
    source: filterSource,
    style: new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255, 255, 255, 0.5)'
        }),
        stroke: new ol.style.Stroke({
            color: '#ffcc33',
            width: 2
        }),
        image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({
                color: '#ffcc33'
            })
        })
    })
});

var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');

var overlay = new ol.Overlay(({
    element: container,
    autoPan: true,
    autoPanAnimation: {
        duration: 250
    }
}));

我更新了 https:/github.comtbremeyerCefSharp.MinimalExample。 以反映这些变化。

© www.soinside.com 2019 - 2024. All rights reserved.