簡體   English   中英

OpenLayers重新投影

[英]OpenLayers reprojection

我正在使用openLayers來顯示來自WMS的一大堆烏拉圭圖層。.我試圖添加可以使用兩個不同基本圖層的選項。 其中之一是位於球形墨卡托900913中的Google衛星圖層。然后,我在UTM21S 32721中有一個烏拉圭地圖。我的問題似乎是當我嘗試更改基本圖層時。 當我顯示Google衛星時,我添加到地圖上的wms圖層(例如烏拉圭的路線)似乎消失了。 當我嘗試另一種方法時(在UTM21S上加載圖層並更改為Google衛星),會發生相同的事情。為了解決此問題,我嘗試監聽更改基礎層的事件。這是代碼:

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

由評論組成

如果使用的投影不是EPSG:4326或EPSG:900913(默認情況下受支持),則必須在OpenLayers之前包含proj4js 該庫將處理投影之間的所有轉換。 更改圖層中的投影並不是一件容易的事,在Layer類中也有很多東西與之耦合。 也許您的方法不起作用,因為設置屬性后,仍然有一些對舊投影的引用。 如果使用addOptions ,則應該能夠有效且安全地在圖層上設置屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM