OpenLayers OpenLayers

Ticket #1036: projection-displayprojection.patch

File projection-displayprojection.patch, 10.2 kB (added by crschmidt, 1 year ago)

support displayProjection -- map, controls

  • tests/Control/test_MousePosition.html

    old new  
    11<html> 
    22<head> 
    33  <script src="../../lib/OpenLayers.js"></script> 
    4   <script type="text/javascript"> 
    5  
    6     function test_MousePosition_constructor(t) { 
    7         t.plan(2); 
     4  <script type="text/javascript"><!-- 
     5    var map, control;  
     6    function test_01_Control_MousePosition_constructor (t) { 
     7        t.plan( 2 ); 
    88     
    9         var control = new OpenLayers.Control.MousePosition(); 
    10         t.ok(control instanceof OpenLayers.Control.MousePosition, "new OpenLayers.Control.MousePosition returns object"); 
    11         t.eq(control.displayClass, "olControlMousePosition", "displayClass set correctly"); 
     9        control = new OpenLayers.Control.MousePosition(); 
     10        t.ok( control instanceof OpenLayers.Control.MousePosition, "new OpenLayers.Control returns object" ); 
     11        t.eq( control.displayClass,  "olControlMousePosition", "displayClass is correct" ); 
    1212    } 
    13  
     13    function test_02_Control_MousePosition_redraw_noLayer_displayProjection(t) { 
     14        t.plan(2); 
     15        control = new OpenLayers.Control.MousePosition({'displayProjection': new OpenLayers.Projection("WGS84")}); 
     16        map = new OpenLayers.Map('map'); 
     17        map.addControl(control); 
     18        control.redraw({'xy': new OpenLayers.Pixel(10,10)}); 
     19        control.redraw({'xy': new OpenLayers.Pixel(12,12)}); 
     20        t.eq(control.div.innerHTML, "", "innerHTML set correctly"); 
     21        l = new OpenLayers.Layer('name', {'isBaseLayer': true}); 
     22        map.addLayer(l); 
     23        map.zoomToMaxExtent(); 
     24        control.redraw({'xy': new OpenLayers.Pixel(10,10)}); 
     25        control.redraw({'xy': new OpenLayers.Pixel(12,12)}); 
     26        t.eq(control.div.innerHTML, "-175.78125, 85.78125", "innerHTML set correctly when triggered."); 
     27    } 
    1428    function test_MousePosition_destroy(t) { 
    1529        t.plan(1); 
    1630     
     
    3650        t.eq(parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Control div zIndexed properly" ); 
    3751        t.eq(parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 5, "Viewport div contains control div"); 
    3852    } 
    39  
     53  // --> 
    4054  </script> 
    4155</head> 
    4256<body> 
  • lib/OpenLayers/Control/MousePosition.js

    old new  
    5050     * {<OpenLayers.LonLat>} 
    5151     */ 
    5252    lastXy: null, 
     53 
     54    /** 
     55     * APIProperty: displayProjection 
     56     * {<OpenLayers.Projection>} A projection that the  
     57     * mousecontrol will display. 
     58     */ 
     59    displayProjection: null,  
    5360     
    5461    /** 
    5562     * Constructor: OpenLayers.Control.MousePosition 
     
    107114                return; 
    108115            } 
    109116 
    110             lonLat = this.map.getLonLatFromPixel(evt.xy); 
     117            var lonLat = this.map.getLonLatFromPixel(evt.xy); 
     118            if (!lonLat) {  
     119                // map has not yet been properly initialized 
     120                return; 
     121            }     
     122            if (this.displayProjection) { 
     123                var mapPosition = OpenLayers.Projection.transform( 
     124                  { x: lonLat.lon, y: lonLat.lat },  
     125                  this.map.getProjectionObject(),  
     126                  this.displayProjection ); 
     127                lonLat.lon = mapPosition.x;   
     128                lonLat.lat = mapPosition.y;   
     129            }       
    111130            this.lastXy = evt.xy; 
     131             
    112132        } 
    113133         
    114134        var digits = parseInt(this.numdigits); 
  • lib/OpenLayers/Control/Permalink.js

    old new  
    2525     */ 
    2626    base: '', 
    2727 
     28    /**  
     29     * APIProperty: displayProjection 
     30     * {<OpenLayers.Projection>} Requires proj4js support.  Projection used 
     31     *     when creating the coordinates in the link. This will reproject the 
     32     *     map coordinates into display coordinates. If you are using this 
     33     *     functionality, the permalink which is last added to the map will 
     34     *     determine the coordinate type which is read from the URL, which 
     35     *     means you should not add permalinks with different 
     36     *     displayProjections to the same map.  
     37     */ 
     38    displayProjection: null,  
     39 
    2840    /** 
    2941     * Constructor: OpenLayers.Control.Permalink 
    3042     * 
     
    6779        for(var i=0; i< this.map.controls.length; i++) { 
    6880            var control = this.map.controls[i]; 
    6981            if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") { 
     82                 
     83                // If a permalink is added to the map, and an ArgParser already 
     84                // exists, we override the displayProjection to be the one 
     85                // on the permalink.  
     86                if (control.displayProjection != this.displayProjection) { 
     87                    this.displayProjection = control.displayProjection; 
     88                }     
     89                 
    7090                break; 
    7191            } 
    7292        } 
    7393        if (i == this.map.controls.length) { 
    74             this.map.addControl(new OpenLayers.Control.ArgParser());        
     94            this.map.addControl(new OpenLayers.Control.ArgParser( 
     95                { 'displayProjection': this.displayProjection }));        
    7596        } 
    7697 
    7798    }, 
     
    113134        var params = OpenLayers.Util.getParameters(this.base); 
    114135         
    115136        params.zoom = this.map.getZoom();  
    116         params.lat = Math.round(center.lat*100000)/100000; 
    117         params.lon = Math.round(center.lon*100000)/100000; 
    118  
     137        var lat = center.lat; 
     138        var lon = center.lon; 
     139         
     140        if (this.displayProjection) { 
     141            var mapPosition = OpenLayers.Projection.transform( 
     142              { x: lon, y: lat },  
     143              this.map.getProjectionObject(),  
     144              this.displayProjection ); 
     145            lon = mapPosition.x;   
     146            lat = mapPosition.y;   
     147        }        
     148        params.lat = Math.round(lat*100000)/100000; 
     149        params.lon = Math.round(lon*100000)/100000; 
     150         
    119151        params.layers = ''; 
    120152        for(var i=0; i< this.map.layers.length; i++) { 
    121153            var layer = this.map.layers[i]; 
  • lib/OpenLayers/Control/ArgParser.js

    old new  
    3030     * {Array(<OpenLayers.Layer>)} 
    3131     */ 
    3232    layers: null, 
     33     
     34    /**  
     35     * APIProperty: displayProjection 
     36     * {<OpenLayers.Projection>} Requires proj4js support.  
     37     *     Projection used when reading the coordinates from the URL. This will 
     38     *     reproject the map coordinates from the URL into the map's 
     39     *     projection. 
     40     * 
     41     *     If you are using this functionality, be aware that any permalink 
     42     *     which is added to the map will determine the coordinate type which 
     43     *     is read from the URL, which means you should not add permalinks with 
     44     *     different displayProjections to the same map.  
     45     */ 
     46    displayProjection: null,  
    3347 
    3448    /** 
    3549     * Constructor: OpenLayers.Control.ArgParser 
     
    5670            var control = this.map.controls[i]; 
    5771            if ( (control != this) && 
    5872                 (control.CLASS_NAME == "OpenLayers.Control.ArgParser") ) { 
     73                 
     74                // If a second argparser is added to the map, then we  
     75                // override the displayProjection to be the one added to the 
     76                // map.  
     77                if (control.displayProjection != this.displayProjection) { 
     78                    this.displayProjection = control.displayProjection; 
     79                }     
     80                 
    5981                break; 
    6082            } 
    6183        } 
     
    97119            //dont need to listen for this one anymore 
    98120            this.map.events.unregister('changebaselayer', this,  
    99121                                       this.setCenter); 
    100                                         
     122             
     123            if (this.displayProjection) { 
     124                var mapPosition = OpenLayers.Projection.transform( 
     125                  { x: this.center.lon, y: this.center.lat },  
     126                  this.displayProjection,  
     127                  this.map.getProjectionObject());  
     128                this.center.lon = mapPosition.x;   
     129                this.center.lat = mapPosition.y;   
     130            }       
     131 
    101132            this.map.setCenter(this.center, this.zoom); 
    102133        } 
    103134    }, 
  • lib/OpenLayers/Map.js

    old new  
    232232     *          stylesheets or style declarations directly in your page. 
    233233     */ 
    234234    theme: null, 
     235     
     236    /**  
     237     * APIProperty: displayProjection 
     238     * {<OpenLayers.Projection>} Requires proj4js support.Projection used by 
     239     *     several controls to display data to user. If this property is set, 
     240     *     it will be set on any control which has a null displayProjection 
     241     *     property at the time the control is added to the map.  
     242     */ 
     243    displayProjection: null, 
    235244 
    236245    /** 
    237246     * APIProperty: fallThrough 
     
    751760        // If a control doesn't have a div at this point, it belongs in the 
    752761        // viewport. 
    753762        control.outsideViewport = (control.div != null); 
     763         
     764        // If the map has a displayProjection, and the control doesn't, set  
     765        // the display projection. 
     766        if (this.displayProjection && !control.displayProjection) { 
     767            control.displayProjection = this.displayProjection; 
     768        }     
     769         
    754770        control.setMap(this); 
    755771        var div = control.draw(px); 
    756772        if (div) {