我正在使用 openLayers 来显示来自 WMS 的一大堆乌拉圭图层。我正在尝试添加可以使用两个不同基础图层的选项。其中之一是位于球形墨卡托 900913 中的谷歌卫星层。然后我有一张位于 UTM21S 32721 中的乌拉圭地图。我的问题似乎是当我尝试更改基础层时。当我显示谷歌卫星时,我添加到地图中的 wms 图层(例如乌拉圭的路线)似乎消失了。当我尝试另一种方式时,同样的事情发生了,在 UTM21S 上加载图层并更改为谷歌卫星。为了解决这个问题,我尝试监听更改基础层的事件。这是代码:
function mapBaseLayerChanged(event) {
var pseudo = new OpenLayers.Projection('EPSG:900913');
var utm21s = new OpenLayers.Projection('EPSG:32721');
var baseLayer = "EPSG:900913";
if(event.layer.name == "Google Satellite"){
map.projection = pseudo;
map.maxExtent = new OpenLayers.Bounds(-6522200,-4170000,-5890000,-3510000);
}else{
baseLayer = "EPSG:32721";
map.projection = utm21s;
map.maxExtent = new OpenLayers.Bounds(300000, 6100000, 900000, 6750000);
}
for(i = 0 ; i < map.layers.length; i++){
if (map.layers[i].visibility && !map.layers[i].isBaseLayer && !map.layers[i].isVector) { // Refresh visible non base
if(baseLayer == "EPSG:900913"){
map.layers[i].projection = pseudo;
}else{
map.layers[i].projection = utm21s;
}
map.layers[i].redraw(true); // Other layer
}
alert(map.layers[i].projection);
}
//alert(map.getProjection());
map.zoomToMaxExtent();
}
当我运行此代码时,图层的投影似乎发生了变化,但出现了同样的问题..提前致谢!!
更新:
试图使它与此一起使用,但没有:
if(baseLayer == "EPSG:900913"){
map.layers[i].addOptions({
srs: 'EPSG:900913',
format:'png',
trnsparent: true,
},true);
//map.layers[i].projection = pseudo;
}else{
map.layers[i].addOptions({
srs: 'EPSG:32721',
format:'png',
trnsparent: true,
},true);
//map.layers[i].projection = utm21s;
}
将参数 srs 更改为 projection 就可以了。现在函数的代码是:
function mapBaseLayerChanged(event) {
var pseudo = new OpenLayers.Projection('EPSG:900913');
var utm21s = new OpenLayers.Projection('EPSG:32721');
var baseLayer = "EPSG:900913";
if(event.layer.name == "Google Satellite"){
map.projection = pseudo;
map.maxExtent = new OpenLayers.Bounds(-6522200,-4170000,-5890000,-3510000);
}else{
baseLayer = "EPSG:32721";
map.projection = utm21s;
map.maxExtent = new OpenLayers.Bounds(300000, 6100000, 900000, 6750000);
}
for(i = 0 ; i < map.layers.length; i++){
if (map.layers[i].visibility && !map.layers[i].isBaseLayer && !map.layers[i].isVector) { // Refresh visible non base
if(baseLayer == "EPSG:900913"){
map.layers[i].addOptions({
projection: pseudo,
format:'png',
trnsparent: true,
},true);
}else{
map.layers[i].addOptions({
projection: utm21s,
format:'png',
trnsparent: true,
},true);
}
}
}
map.zoomToMaxExtent();
}