加载GeoServer WMS服务时,地图无法加载,出现错误:
View.js:1552 Uncaught TypeError: Cannot read property 'slice' of null
at xs (View.js:1552)
at e.applyOptions_ (View.js:378)
at new e (View.js:330)
at test.html:115
我加载服务的代码是:
var imagery = new ol.layer.Image({
source: new ol.source.ImageWMS({
ratio: 1,
url: 'http://localhost:8999/geoserver/dem/wms',
params: {
'FORMAT': 'image/jpeg',//'image/jpeg',//
'VERSION': '1.1.1',
"STYLES": '',
"LAYERS": 'dem:hhu_fill_dem',
"exceptions": 'application/vnd.ogc.se_inimage',
},
crossOrigin:''
})
});
var projection = new ol.proj.Projection({
code: 'EPSG:3857',
units: 'm',
global: true
});
var map = new ol.Map({
controls: ol.control.defaults({
attribution: false
}).extend([mousePositionControl]),
target: container,
layers: [
imagery
],
view: new ol.View({
projection: projection,
}),
});
错误的地方是加载视图这一块view: new ol.View({ projection: projection}),
,一步步排除后发现问题在:
var projection = new ol.proj.Projection({
code: 'EPSG:3857',
units: 'm',
global: true
});
其中的global: true
有问题,global
的默认属性为false
,将其注释掉,发现不报错,地图也能正常加载出来了。
关于global的官方解释为:
Whether the projection is valid for the whole globe.
然而,在geoserver加载该服务的源代码中,是将global设为true的,并且成功加载,不清楚这是为什么??