[OpenLayers-Users] Can I make the map cover the whole viewport

Pascoe, S (Stephen) S.Pascoe at rl.ac.uk
Wed Feb 28 09:53:45 EST 2007


> Hi,
> I have to fill the whole viewport with the map. I don't mind panning
if
> necessary to view the whole map, but Iam required not to show any
empty area
> around the map. So everytime the user increases the viewport size, I
need to
> zoom my map automatically so that it covers the entire viewport. How
can I
> achieve this?
 
I think your problem might be slightly different but I recently found a
solution to preventing users panning the map until it didn't fill the
viewport.  I achieved it by inheriting from OpenLayers.Map and
overloading setCenter:
 
DDCVisMap = OpenLayers.Class.create();
DDCVisMap.prototype = OpenLayers.Class.inherit(OpenLayers.Map, {
    setCenter: function(center, zoom, dragging) {
 
        if (center == null) {
            center = this.getCenter();
        }                
        if (zoom == null) {
            zoom = this.getZoom();
        }
 
 var resolution = this.baseLayer.resolutions[zoom];
 var size = this.getSize();
 var w_deg = size.w * resolution;
 var h_deg = size.h * resolution;
        
 var bounds = new OpenLayers.Bounds(center.lon - w_deg / 2,
        center.lat - h_deg / 2,
        center.lon + w_deg / 2,
        center.lat + h_deg / 2);
 
 if (bounds.left < -180.0) {
     center.lon = center.lon + (-180.0 - bounds.left);
 }
 else if (bounds.right > 180.0) {
     center.lon = center.lon - (bounds.right - 180.0);
 }
 
 if (bounds.bottom < -90.0) {
     center.lat = center.lat + (-90.0 - bounds.bottom);
 }
 else if (bounds.top > 90.0) {
     center.lat = center.lat - (bounds.top - 90.0);
 }
 
 OpenLayers.Map.prototype.setCenter.apply(this, [center, zoom,
dragging]);
    }
});
 
I'm an openlayers newby so it might not be very idiomatic but provided
the maxExtent fills the viewport it seems to work.  I'm using
OpenLayers-2.3-rc2.
 
---
Stephen Pascoe  01235 445980
British Atmospheric Data Centre
Rutherford Appleton Laboratory, CCLRC
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20070228/16e6eb45/attachment.html


More information about the Users mailing list