Javascript 如何在OpenLayers中独立移动两个层?

Javascript 如何在OpenLayers中独立移动两个层?,javascript,maps,gis,openlayers,Javascript,Maps,Gis,Openlayers,可以在OpenLayers中创建两个层(其中一个是半透明的)并单独移动它们吗?如果是,怎么做? 我想让用户选择移动哪一层,或者如果不可能,通过我自己的JavaScript代码移动一层,而另一层由用户控制 如果这很重要的话,这两个都将是预渲染的pixmap层。这就是我提出的解决方案。它不漂亮,但对我来说很有用 更好的选择是非常受欢迎的 /** * @requires OpenLayers/Layer/TMS.js */ MyLayer = OpenLayers.Class(OpenLayers.L

可以在OpenLayers中创建两个层(其中一个是半透明的)并单独移动它们吗?如果是,怎么做?
我想让用户选择移动哪一层,或者如果不可能,通过我自己的JavaScript代码移动一层,而另一层由用户控制


如果这很重要的话,这两个都将是预渲染的pixmap层。

这就是我提出的解决方案。它不漂亮,但对我来说很有用

更好的选择是非常受欢迎的

/**
* @requires OpenLayers/Layer/TMS.js
*/
MyLayer = OpenLayers.Class(OpenLayers.Layer.TMS, {
    latShift: 0.0,
    latShiftPx: 0,

    setMap: function(map) {
        OpenLayers.Layer.TMS.prototype.setMap.apply(this, arguments);
        map.events.register("moveend", this, this.mapMoveEvent)
    },
    // This is the function you will want to modify for your needs
    mapMoveEvent: function(event) {
        var resolution = this.map.getResolution();
        var center = this.map.getCenter();

        // This is some calculation I use, replace it whatever you like:
        var h = center.clone().transform(projmerc, proj4326);
        var elliptical = EllipticalMercator.fromLonLat(h.lon, h.lat);
        var myCenter = new OpenLayers.LonLat(elliptical.x, elliptical.y);

        this.latShift = myCenter.lat - center.lat;
        this.latShiftPx = Math.round(this.latShift/resolution);

        this.div.style.top = this.latShiftPx + "px";
    },
    moveTo: function(bounds, zoomChanged, dragging) {
        bounds = bounds.add(0, this.latShift);
        OpenLayers.Layer.TMS.prototype.moveTo.apply(this, [bounds, zoomChanged, dragging]);
    },
    // mostly copied and pasted from Grid.js ...
    moveGriddedTiles: function() {
        var buffer = this.buffer + 1;
        while(true) {
            var tlTile = this.grid[0][0];
            var tlViewPort = {
                x: tlTile.position.x +
                    this.map.layerContainerOriginPx.x,
                y: tlTile.position.y +
                    this.map.layerContainerOriginPx.y + this.latShiftPx // ... except this line
            };
            var ratio = this.getServerResolution() / this.map.getResolution();
            var tileSize = {
                w: Math.round(this.tileSize.w * ratio),
                h: Math.round(this.tileSize.h * ratio)
            };
            if (tlViewPort.x > -tileSize.w * (buffer - 1)) {
                this.shiftColumn(true, tileSize);
            } else if (tlViewPort.x < -tileSize.w * buffer) {
                this.shiftColumn(false, tileSize);
            } else if (tlViewPort.y > -tileSize.h * (buffer - 1)) {
                this.shiftRow(true, tileSize);
            } else if (tlViewPort.y < -tileSize.h * buffer) {
                this.shiftRow(false, tileSize);
            } else {
                break;
            }
        }
    },
    CLASS_NAME: "MyLayer"
});
/**
*@需要OpenLayers/Layer/TMS.js
*/
MyLayer=OpenLayers.Class(OpenLayers.Layer.TMS{
latShift:0.0,
latShiftPx:0,
setMap:函数(map){
OpenLayers.Layer.TMS.prototype.setMap.apply(这是参数);
map.events.register(“moveend”,this,this.mapmovevent)
},
//这是您需要根据需要修改的功能
mapMoveEvent:函数(事件){
var resolution=this.map.getResolution();
var center=this.map.getCenter();
//这是我使用的一些计算,请随意替换:
var h=center.clone().transform(projmerc,proj4326);
var椭圆=来自LONLAT(h.lon,h.lat)的椭圆检流计;
var myCenter=newopenlayers.LonLat(椭圆x,椭圆y);
this.latShift=myCenter.lat-center.lat;
this.latShiftPx=Math.round(this.latShift/分辨率);
this.div.style.top=this.latShiftPx+“px”;
},
moveTo:函数(边界、缩放更改、拖动){
bounds=bounds.add(0,this.latShift);
OpenLayers.Layer.TMS.prototype.moveTo.apply(这个[bounds,zoomChanged,Draging]);
},
//主要是从Grid.js复制和粘贴的。。。
moveGriddedTiles:function(){
var buffer=this.buffer+1;
while(true){
var tlTile=this.grid[0][0];
变量tlViewPort={
x:tlTile.position.x+
这个.map.layerContainerOriginPx.x,
y:tlTile.position.y+
this.map.layerContainerOriginPx.y+this.latShiftPx/…除此行外
};
var ratio=this.getServerResolution()/this.map.getResolution();
变量tileSize={
w:Math.round(这个.tileSize.w*比率),
h:Math.round(这个.tileSize.h*比率)
};
if(tlViewPort.x>-tileSize.w*(缓冲区-1)){
此.shiftColumn(true,tileSize);
}else if(tlViewPort.x<-tileSize.w*缓冲区){
此.shiftColumn(false,tileSize);
}else if(tlViewPort.y>-tileSize.h*(缓冲区-1)){
this.shiftRow(true,tileSize);
}else if(tlViewPort.y<-tileSize.h*缓冲区){
this.shiftRow(false,tileSize);
}否则{
打破
}
}
},
类别名称:“MyLayer”
});

请注意,这只适用于OpenLayers 2.13或更高版本

,这肯定不是我尝试过的。你有没有想过在gis.stackexchange.com上问这个问题?有更多的OpenLayers开发者在那里,你可能会得到更多的运气得到答案。我甚至不知道该网站存在,所以谢谢!我会试着问一下。