OpenLayers OpenLayers

Ticket #482: tile_bounds.patch

File tile_bounds.patch, 1.5 kB (added by tschaub, 2 years ago)

calculate tile bounds given a viewport pixel location

  • Grid.js

    old new  
    434434        } 
    435435    }, 
    436436     
     437    /** 
     438     * 
     439     * @param {OpenLayers.Pixel} viewPortPx The location in the viewport 
     440     * 
     441     * @returns The tile bounds for a layer given a pixel location 
     442     * @type OpenLayers.Bounds 
     443     */ 
     444    getTileBounds: function(viewPortPx) { 
     445        var maxExtent = this.map.getMaxExtent(); 
     446        var resolution = this.getResolution(); 
     447        var tileMapWidth = resolution * this.tileSize.w; 
     448        var tileMapHeight = resolution * this.tileSize.h; 
     449        var mapPoint = this.getLonLatFromViewPortPx(viewPortPx); 
     450        var tileLeft = maxExtent.left + (tileMapWidth * 
     451                                         Math.floor((mapPoint.lon - 
     452                                                     maxExtent.left) / 
     453                                                    tileMapWidth)); 
     454        var tileBottom = maxExtent.bottom + (tileMapHeight * 
     455                                             Math.floor((mapPoint.lat - 
     456                                                         maxExtent.bottom) / 
     457                                                        tileMapHeight)); 
     458        return new OpenLayers.Bounds(tileLeft, tileBottom, 
     459                                     tileLeft + tileMapWidth, 
     460                                     tileBottom + tileMapHeight); 
     461    }, 
     462     
    437463    /** @final @type String */ 
    438464    CLASS_NAME: "OpenLayers.Layer.Grid" 
    439465});