OpenLayers OpenLayers

Changeset 4268

Show
Ignore:
Timestamp:
09/13/07 16:36:36 (1 year ago)
Author:
elemoine
Message:

allow user to specify offsets for externalGraphic (closes #893)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/vector-features.html

    r4222 r4268  
    5959            style_mark.graphicWidth = 24; 
    6060            style_mark.graphicHeight = 20; 
     61            style_mark.graphicXOffset = -(style_mark.graphicWidth/2);  // this is the default value 
     62            style_mark.graphicYOffset = -style_mark.graphicHeight; 
    6163            style_mark.externalGraphic = "../img/marker.png"; 
    6264             
  • trunk/openlayers/lib/OpenLayers/Feature/Vector.js

    r4114 r4268  
    264264 *  - graphicHeight, 
    265265 *  - graphicOpacity 
     266 *  - graphicXOffset 
     267 *  - graphicYOffset 
    266268 */  
    267269OpenLayers.Feature.Vector.style = { 
  • trunk/openlayers/lib/OpenLayers/Renderer/SVG.js

    r4114 r4268  
    185185                // remove old node 
    186186                var id = node.getAttributeNS(null, "id"); 
    187                 var x = node.getAttributeNS(null, "cx"); 
    188                 var y = node.getAttributeNS(null, "cy"); 
     187                var x = parseFloat(node.getAttributeNS(null, "cx")); 
     188                var y = parseFloat(node.getAttributeNS(null, "cy")); 
    189189                var _featureId = node._featureId; 
    190190                var _geometryClass = node._geometryClass; 
     
    207207                width = width ? width : style.pointRadius*2; 
    208208                height = height ? height : style.pointRadius*2; 
     209                var xOffset = (style.graphicXOffset != undefined) ? 
     210                    style.graphicXOffset : -(0.5 * width); 
     211                var yOffset = (style.graphicYOffset != undefined) ? 
     212                    style.graphicYOffset : -(0.5 * height); 
    209213                var opacity = style.graphicOpacity || style.fillOpacity; 
    210214                 
    211                 node.setAttributeNS(null, "x", x-(.5*width).toFixed()); 
    212                 node.setAttributeNS(null, "y", -y-(.5*height).toFixed()); 
     215                node.setAttributeNS(null, "x", (x + xOffset).toFixed()); 
     216                node.setAttributeNS(null, "y", (-y + yOffset).toFixed()); 
    213217                node.setAttributeNS(null, "width", width); 
    214218                node.setAttributeNS(null, "height", height); 
  • trunk/openlayers/lib/OpenLayers/Renderer/VML.js

    r4114 r4268  
    182182                width = width ? width : style.pointRadius*2; 
    183183                height = height ? height : style.pointRadius*2; 
     184 
    184185                var resolution = this.getResolution(); 
    185                 node.style.left = (geometry.x/resolution-.5*width).toFixed(); 
    186                 node.style.top = (geometry.y/resolution-.5*height).toFixed(); 
     186                var xOffset = (style.graphicXOffset != undefined) ? 
     187                    style.graphicXOffset : -(0.5 * width); 
     188                var yOffset = (style.graphicYOffset != undefined) ? 
     189                    style.graphicYOffset : -(0.5 * height); 
     190                 
     191                node.style.left = ((geometry.x/resolution)+xOffset).toFixed(); 
     192                node.style.top = ((geometry.y/resolution)-(yOffset+height)).toFixed(); 
    187193                node.style.width = width; 
    188194                node.style.height = height;     
  • trunk/openlayers/tests/Layer/test_Vector.html

    r4220 r4268  
    159159 
    160160    function test_Layer_Vector_externalGraphic(t) { 
    161         t.plan(9); 
     161        t.plan(11); 
    162162        // base layer is needed for getResolution() to return a value, 
    163163        // otherwise VML test will fail because style.left and style.top 
     
    170170             
    171171        var layer = new OpenLayers.Layer.Vector("Test Layer"); 
     172        var renderer = layer.renderer; 
    172173        var map = new OpenLayers.Map('map'); 
    173174        map.addLayers([baseLayer, layer]); 
    174175 
    175         var geometry = new OpenLayers.Geometry.Point(10, 10); 
     176        var geometryX = 10; 
     177        var geometryY = 10; 
     178        var geometry = new OpenLayers.Geometry.Point(geometryX, geometryY); 
    176179        var feature = new OpenLayers.Feature.Vector(geometry); 
     180 
     181        map.zoomToMaxExtent(); 
    177182 
    178183        var customStyle1 = new Object({ 
     
    198203                graphicOpacity: 1 
    199204        }); 
     205        var customStyle6 = new Object({ 
     206                externalGraphic: 'test.png', 
     207                graphicWidth: 24, 
     208                graphicHeight: 16, 
     209                graphicXOffset: -24, 
     210                graphicYOffset: -16 
     211        }); 
    200212                
    201         var root = layer.renderer.root; 
     213        var root = renderer.root; 
    202214        if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.SVG') { 
    203215                feature.style = customStyle1; 
     
    238250                            'opacity: '+customStyle5.graphicOpacity.toString()+';', 
    239251                            "graphicOpacity correctly set"); 
     252                feature.style = customStyle6; 
     253                layer.drawFeature(feature); 
     254                var x = geometryX / renderer.getResolution() + renderer.left; 
     255                var y = geometryY / renderer.getResolution() - renderer.top; 
     256                // SVG setStyle() gets x and y using getAttributeNS(), which returns 
     257                // a value with only 3 decimal digits. To mimic this we use toFixed(3) here 
     258                x = x.toFixed(3); 
     259                y = y.toFixed(3); 
     260                // toFixed() returns a string 
     261                x = parseFloat(x); 
     262                y = parseFloat(y); 
     263                t.eq(root.firstChild.getAttributeNS(null, 'x'), 
     264                        (x + customStyle6.graphicXOffset).toFixed().toString(), 
     265                        "graphicXOffset correctly set"); 
     266                t.eq(root.firstChild.getAttributeNS(null, 'y'), 
     267                        (-y + customStyle6.graphicYOffset).toFixed().toString(), 
     268                        "graphicYOffset correctly set"); 
    240269        } 
    241270        if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.VML') { 
     
    280309                            customStyle5.graphicOpacity, 
    281310                            "graphicOpacity correctly set");  
     311                feature.style = customStyle6; 
     312                layer.drawFeature(feature); 
     313                var x = geometryX / renderer.getResolution(); 
     314                var y = geometryY / renderer.getResolution(); 
     315                t.eq(root.firstChild.style.left, 
     316                            (x + customStyle6.graphicXOffset).toFixed().toString()+'px', 
     317                            "graphicXOffset correctly set"); 
     318                             
     319                t.eq(root.firstChild.style.top, 
     320                            (y - (customStyle6.graphicYOffset+parseInt(root.firstChild.style.height))).toFixed().toString()+'px', 
     321                            "graphicYOffset correctly set"); 
    282322 
    283323        }