4

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

如果这很重要,两者都将是预渲染的像素图图层。

4

1 回答 1

2

这是我想出的解决方案。它不漂亮,但它适用于我的目的。

更好的替代品非常受欢迎......

/**
* @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 2.13 或更高版本

于 2013-01-27T12:41:35.470 回答