【发布时间】:2012-08-03 12:58:54
【问题描述】:
我正在使用 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 更改为投影,然后就成功了。现在函数的代码是:
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();
}
【问题讨论】:
-
您是否包含了 Proj4JS?如果我没记错的话,OpenLayers 仅支持 EPSG:900913 和 WGS:84,如果您使用其他投影,则必须包含 Proj4JS。
-
感谢您的回答!是的,已经添加了投影,否则我的乌拉圭图层无法显示..
-
使用addOptions是否有效?也许这会初始化所有需要的东西。
-
没什么,我更新了帖子..
-
将 srs 更改为投影,它起作用了...谢谢 maenu!
标签: javascript openlayers projection