1

I am updating from OpenLayers 3.15.0 to 6.3.1. When I call map.addLayer, I get the following error:

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

Here is the context of where it is called:

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);
};

The function is identical from the one used to call addLayer with 3.15.0 except the properties zIndex and minZoom. In 3.15.0 it was working.

What could be the problem?

Update

I created a MinimalExample solution based on the MinimalExample of CefSharp. You need Visual Studio or Rider to open it. https://github.com/tbremeyer/CefSharp.MinimalExample.git

Currently the call to EvaluateScriptAsync in the function CallWebSite in MainWindows.xaml.cs returns with

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

I would expect it to return with

Message = ""
Success = true
tobre
  • 1,347
  • 3
  • 21
  • 53

1 Answers1

0

The map object did not get initialized properly. Here is the code to create the map:

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({
    })
});

It uses an extend with the mousePositionControl. The mousePositionControl is defined as:

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

So it uses a projection. The projection did not get initialized properly.

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);

From OpenLayers: Map doesn't render with a non 'standard' EPSG-code I learned, that I can use

ol.proj.proj4.register(proj4);

to help with the projection. Now it reads:

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);

After this change the filterVector was missing and the definition of the overlay had to be moved to before the initialization of map.

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
    }
}));

I updated the minimal example at https://github.com/tbremeyer/CefSharp.MinimalExample to reflect these changes.

tobre
  • 1,347
  • 3
  • 21
  • 53