OpenLayers重新投影 [英] OpenLayers reprojection

查看:682
本文介绍了OpenLayers重新投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openLayers来显示来自WMS的一大堆乌拉圭图层..我试图添加可以使用两个不同基本图层的选项. 其中之一是位于球形墨卡托900913中的Google卫星层.然后,我得到了UTM21S 32721中的乌拉圭地图. 我的问题似乎是当我尝试更改基础层时.当我显示Google卫星时,我添加到地图上的wms图层(例如,乌拉圭的路线)似乎消失了.当我尝试另一种方法时,也会发生相同的事情,即在UTM21S上加载图层并更改为Google卫星. 为了解决这个问题,我尝试听一下更改基础层的事件.这是代码:

I'm using openLayers to show a whole bunch of layers of Uruguay, that come from a WMS.. I'm trying to add the option that you can use two different base layers. One of them is the google satellite layer which is in spherical mercator 900913.. Then I have a map of Uruguay which is in UTM21S 32721.. My problem seems to be when I try to change the base layer. The wms layers that I added to the map (for example routes of Uruguay) when I was showing the google satellite seem to disappear. The same thing happens when I try the other way, loading layers on UTM21S and changing to the google satellite.. To address this issue, I have tried to listen to the event of changing the base layer.. Here's the code:

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

当我运行此代码时,层的投影似乎发生了变化,但是发生了同样的问题. 在此先感谢!

When I run this code, the projection of the layers seem to change, but the same problem occurs.. Thanks in advance!!

更新:

试图使其与此一起工作,但一无所获:

Tried to make it work with this but nothing:

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,就成功了..现在,该函数的代码为:

Changed the parameter srs to projection and that did the trick.. The code of the function now is:

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

推荐答案

由评论组成的问题

如果使用的投影不是EPSG:4326或EPSG:900913(默认情况下受支持),则必须包含也可以与Layer 类耦合.也许您的方法不起作用,因为在设置属性后,仍然有一些对旧投影的引用.如果您使用 addOptions ,则应该能够有效,安全地在图层上设置属性.

If are working with projections other than EPSG:4326 or EPSG:900913 (which are supported by default), you must include proj4js before OpenLayers. This library will handle all transformations between projections. Changing projections in a layer is not trivial, there is a lot of stuff inside the Layer class that is coupled too it. Maybe your approach doesn't work, because there are still some references left to the old projection after you have set the property. If you use addOptions, you should be able to set properties on the layer effectively and safe.

这篇关于OpenLayers重新投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆