OpenLayers reprojection

2019-09-15 18:00发布

问题:

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!!

Update:

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

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

回答1:

Composed from comments to the question

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.