OpenLayers OpenLayers

Changeset 329

Show
Ignore:
Timestamp:
05/24/06 14:45:50 (3 years ago)
Author:
euzuro
Message:

fix for #60

conversions to/from lonlat/px need to take into account
the offset of the layersContainerDiv.

I have introduced the following functions for converting
between layer and screen pixel values:

getLayerPxFromScreenPx() and getScreenPxFromLayerPx()

they are pretty self-explanitory.

I then renamed:

getPixelFromLonLat() and getLonLatFromPixel()

to:

getScreenPxFromLonLat() and getLonLatFroScreenmPx()

and added:

getLayerPxFromLonLat() and getLonLatFromLayerPx()

updates were made throughout the code, demos, and tests
so everything should still run smoothly.

-e-

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/markerss.html

    r325 r329  
    99        } 
    1010    </style> 
    11     <script src="lib/OpenLayers.js"></script> 
     11    <script src="../lib/OpenLayers.js"></script> 
    1212    <script type="text/javascript"> 
    1313        <!-- 
     
    2222                {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'} ); 
    2323                 
     24 
     25            map.addLayer( 
     26            new OpenLayers.Layer.WMS("NASA Mosaic",  
     27                         "http://wms.jpl.nasa.gov/wms.cgi", 
     28                         {"EXCEPTIONS" : "application/vnd.ogc.se_inimage", 
     29                          "format" : "image/jpeg", 
     30                          layers:"modis,global_mosaic"} 
     31                         )); 
     32 
    2433            map.addLayer(layer); 
    2534 
     
    3241         
    3342        function changer() { 
    34      
    35  
     43            var lon = map.getLonLatFromLayerPx(new OpenLayers.Pixel(0,0)).lon; 
     44            var lat = map.getLonLatFromLayerPx(new OpenLayers.Pixel(0,0)).lat; 
     45            var slon = map.getLonLatFromScreenPx(new OpenLayers.Pixel(0,0)).lon; 
     46            var slat = map.getLonLatFromScreenPx(new OpenLayers.Pixel(0,0)).lat; 
     47            alert("lon=" + lon + " lat=" + lat  
     48                  + "slon=" + slon + " slat=" + slat); 
    3649        } 
    3750 
    3851        function add() { 
    3952            var icon = new OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',new OpenLayers.Size(10,17)); 
    40             marker = new OpenLayers.Marker(new OpenLayers.LonLat(180,90), icon); 
     53            marker = new OpenLayers.Marker(new OpenLayers.LonLat(0,00), icon); 
    4154            markers.addMarker(marker); 
    4255             
     
    5972    <div id="map"></div> 
    6073    <div style="background-color:purple" onclick="add()"> click to add popup to map</div> 
    61     <div style="background-color:blue" onclick="changer()"> click to modify popup's attributes</div> 
     74    <div style="background-color:blue" onclick="changer()"> click to get lon/lat for pixel point(0,0)</div> 
    6275    <div style="background-color:red" onclick="destroy()"> click to destroy the popup</div> 
    6376    <div style="background-color:green" onclick="remove()"> click to remove the popup from map</div> 
  • trunk/openlayers/lib/OpenLayers/Control/MouseDefaults.js

    r326 r329  
    2020    */ 
    2121    defaultDblClick: function (evt) { 
    22         var newCenter = this.map.getLonLatFromPixel( evt.xy );  
     22        var newCenter = this.map.getLonLatFromScreenPx( evt.xy );  
    2323        this.map.setCenter(newCenter, this.map.zoom + 1); 
    2424    }, 
     
    6969                var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, 
    7070                                                 size.h / 2 + deltaY); 
    71                 var newCenter = this.map.getLonLatFromPixel( newXY );  
     71                var newCenter = this.map.getLonLatFromScreenPx( newXY );  
    7272                this.map.setCenter(newCenter); 
    7373                this.mouseDragStart = evt.xy.copyOf(); 
     
    8181    defaultMouseUp: function (evt) { 
    8282        if (this.zoomBox) { 
    83             var start = this.map.getLonLatFromPixel( this.mouseDragStart );  
    84             var end = this.map.getLonLatFromPixel( evt.xy ); 
     83            var start = this.map.getLonLatFromScreenPx( this.mouseDragStart );  
     84            var end = this.map.getLonLatFromScreenPx( evt.xy ); 
    8585            var top = Math.max(start.lat, end.lat); 
    8686            var bottom = Math.min(start.lat, end.lat); 
  • trunk/openlayers/lib/OpenLayers/Layer/Markers.js

    r326 r329  
    5959    */ 
    6060    drawMarker: function(marker) { 
    61         var px = this.map.getPixelFromLonLat(marker.lonlat); 
     61        var px = this.map.getLayerPxFromLonLat(marker.lonlat); 
    6262        var markerImg = marker.draw(px); 
    6363        if (!marker.drawn) { 
  • trunk/openlayers/lib/OpenLayers/Map.js

    r328 r329  
    172172    addPopup: function(popup) { 
    173173        this.popups.push(popup); 
    174         var px = this.getPixelFromLonLat(popup.lonlat) 
     174        var px = this.getLayerPxFromLonLat(popup.lonlat) 
    175175        var popupDiv = popup.draw(px); 
    176176        if (popupDiv) { 
     
    275275     
    276276    /** 
    277     * @param {OpenLayers.Pixel} point 
     277     * @param {OpenLayers.Pixel} layerPx 
     278     *  
     279     * @returns px translated into screen pixel coordinates 
     280     * @type OpenLayers.Pixel 
     281     */ 
     282    getScreenPxFromLayerPx:function(layerPx) { 
     283        var screenPx = layerPx.copyOf(); 
     284 
     285        screenPx.x += parseInt(this.layerContainerDiv.style.left); 
     286        screenPx.y += parseInt(this.layerContainerDiv.style.top); 
     287 
     288        return screenPx; 
     289    }, 
     290     
     291    /** 
     292     * @param {OpenLayers.Pixel} screenPx 
     293     *  
     294     * @returns px translated into screen pixel coordinates 
     295     * @type OpenLayers.Pixel 
     296     */ 
     297    getLayerPxFromScreenPx:function(screenPx) { 
     298        var layerPx = screenPx.copyOf(); 
     299 
     300        layerPx.x -= parseInt(this.layerContainerDiv.style.left); 
     301        layerPx.y -= parseInt(this.layerContainerDiv.style.top); 
     302 
     303        return layerPx; 
     304    }, 
     305 
     306 
     307    /** 
     308    * @param {OpenLayers.Pixel} px 
    278309    * 
    279310    * @return {OpenLayers.LonLat}  
    280311    */ 
    281     getLonLatFromPixel: function (point) { 
     312    getLonLatFromLayerPx: function (px) { 
     313       //adjust for displacement of layerContainerDiv 
     314       px = this.getScreenPxFromLayerPx(px); 
     315       return this.getLonLatFromScreenPx(px);          
     316    }, 
     317     
     318    /** 
     319    * @param {OpenLayers.Pixel} screenPx 
     320    * 
     321    * @returns An OpenLayers.LonLat which is the passed-in screen  
     322    *          OpenLayers.Pixel, translated into lon/lat given the  
     323    *          current extent and resolution 
     324    * @type OpenLayers.LonLat 
     325    */ 
     326    getLonLatFromScreenPx: function (screenPx) { 
    282327        var center = this.getCenter();        //map center lon/lat 
    283328        var res  = this.getResolution(); 
    284329        var size = this.getSize(); 
    285330     
    286         var delta_x = point.x - (size.w / 2); 
    287         var delta_y = point.y - (size.h / 2); 
     331        var delta_x = screenPx.x - (size.w / 2); 
     332        var delta_y = screenPx.y - (size.h / 2); 
    288333         
    289334        return new OpenLayers.LonLat(center.lon + delta_x * res , 
    290335                                     center.lat - delta_y * res);  
     336    }, 
     337 
     338    /** 
     339    * @param {OpenLayers.LonLat} lonlat 
     340    * 
     341    * @returns An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat,  
     342    *          translated into layer pixels given the current extent  
     343    *          and resolution 
     344    * @type OpenLayers.Pixel 
     345    */ 
     346    getLayerPxFromLonLat: function (lonlat) { 
     347       //adjust for displacement of layerContainerDiv 
     348       var px = this.getScreenPxFromLonLat(lonlat); 
     349       return this.getLayerPxFromScreenPx(px);          
    291350    }, 
    292351 
     
    299358    * @type OpenLayers.Pixel 
    300359    */ 
    301     getPixelFromLonLat: function (lonlat) { 
     360    getScreenPxFromLonLat: function (lonlat) { 
    302361        var resolution = this.getResolution(); 
    303362        var extent = this.getExtent(); 
     
    337396            for (var i = 0; i < this.popups.length; i++) { 
    338397                var popup = this.popups[i]; 
    339                 var px = this.getPixelFromLonLat(popup.lonlat); 
     398                var px = this.getLayerPxFromLonLat(popup.lonlat); 
    340399                popup.moveTo(px); 
    341400            } 
  • trunk/openlayers/tests/test_Map.html

    r315 r329  
    126126  
    127127        var pixel = new OpenLayers.Pixel(50,150); 
    128         var lonlat = map.getLonLatFromPixel(pixel); 
    129         t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromPixel returns valid OpenLayers.LonLat" ); 
     128        var lonlat = map.getLonLatFromScreenPx(pixel); 
     129        t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromScreenPx returns valid OpenLayers.LonLat" ); 
    130130 
    131         var newPixel = map.getPixelFromLonLat(lonlat); 
    132         t.ok( newPixel instanceof OpenLayers.Pixel, "getPixelFromLonLat returns valid OpenLayers.Pixel" ); 
     131        var newPixel = map.getScreenPxFromLonLat(lonlat); 
     132        t.ok( newPixel instanceof OpenLayers.Pixel, "getScreenPxFromLonLat returns valid OpenLayers.Pixel" ); 
    133133 
    134134        // WARNING!!!  I'm faily sure that the following test's validity