OpenLayers OpenLayers

Ticket #893: patch-893-A2.diff

File patch-893-A2.diff, 7.7 kB (added by elemoine, 10 months ago)
  • tests/Layer/test_Vector.html

    old new  
    157157    } 
    158158 
    159159    function test_Layer_Vector_externalGraphic(t) { 
    160         t.plan(9); 
     160        t.plan(11); 
    161161        // base layer is needed for getResolution() to return a value, 
    162162        // otherwise VML test will fail because style.left and style.top 
    163163        // cannot be set 
     
    168168               format: 'image/png'}); 
    169169             
    170170        var layer = new OpenLayers.Layer.Vector("Test Layer"); 
     171        var renderer = layer.renderer; 
    171172        var map = new OpenLayers.Map('map'); 
    172173        map.addLayers([baseLayer, layer]); 
    173174 
    174         var geometry = new OpenLayers.Geometry.Point(10, 10); 
     175        var geometryX = 10; 
     176        var geometryY = 10; 
     177        var geometry = new OpenLayers.Geometry.Point(geometryX, geometryY); 
    175178        var feature = new OpenLayers.Feature.Vector(geometry); 
    176179 
     180        map.zoomToMaxExtent(); 
     181 
    177182        var customStyle1 = new Object({ 
    178183                externalGraphic: 'test.png', 
    179184                pointRadius: 10 
     
    196201                graphicWidth: 24, 
    197202                graphicOpacity: 1 
    198203        }); 
     204        var customStyle6 = new Object({ 
     205                externalGraphic: 'test.png', 
     206                graphicWidth: 24, 
     207                graphicHeight: 16, 
     208                graphicXOffset: -24, 
     209                graphicYOffset: -16 
     210        }); 
    199211                
    200         var root = layer.renderer.root; 
     212        var root = renderer.root; 
    201213        if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.SVG') { 
    202214                feature.style = customStyle1; 
    203215                layer.drawFeature(feature); 
     
    236248                t.eq(root.firstChild.getAttributeNS(null, 'style'), 
    237249                            'opacity: '+customStyle5.graphicOpacity.toString()+';', 
    238250                            "graphicOpacity correctly set"); 
     251                feature.style = customStyle6; 
     252                layer.drawFeature(feature); 
     253                var x = geometryX / renderer.getResolution() + renderer.left; 
     254                var y = geometryY / renderer.getResolution() - renderer.top; 
     255                // SVG setStyle() gets x and y using getAttributeNS(), which returns 
     256                // a value with only 3 decimal digits. To mimic this we use toFixed(3) here 
     257                x = x.toFixed(3); 
     258                y = y.toFixed(3); 
     259                // toFixed() returns a string 
     260                x = parseFloat(x); 
     261                y = parseFloat(y); 
     262                t.eq(root.firstChild.getAttributeNS(null, 'x'), 
     263                        (x + customStyle6.graphicXOffset).toFixed().toString(), 
     264                        "graphicXOffset correctly set"); 
     265                t.eq(root.firstChild.getAttributeNS(null, 'y'), 
     266                        (-y + customStyle6.graphicYOffset).toFixed().toString(), 
     267                        "graphicYOffset correctly set"); 
    239268        } 
    240269        if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.VML') { 
    241270                feature.style = customStyle1; 
     
    278307                t.eq(opacity, 
    279308                            customStyle5.graphicOpacity, 
    280309                            "graphicOpacity correctly set");  
     310                feature.style = customStyle6; 
     311                layer.drawFeature(feature); 
     312                var x = geometryX / renderer.getResolution(); 
     313                var y = geometryY / renderer.getResolution(); 
     314                t.eq(root.firstChild.style.left, 
     315                            (x + customStyle6.graphicXOffset).toFixed().toString()+'px', 
     316                            "graphicXOffset correctly set"); 
     317                t.eq(root.firstChild.style.top, 
     318                            (y + customStyle6.graphicYOffset).toFixed().toString()+'px', 
     319                            "graphicYOffset correctly set"); 
    281320 
    282321        } 
    283322    } 
  • lib/OpenLayers/Feature/Vector.js

    old new  
    263263 *  - graphicWidth, 
    264264 *  - graphicHeight, 
    265265 *  - graphicOpacity 
     266 *  - graphicXOffset 
     267 *  - graphicYOffset 
    266268 */  
    267269OpenLayers.Feature.Vector.style = { 
    268270    'default': { 
  • lib/OpenLayers/Renderer/VML.js

    old new  
    182182                width = width ? width : style.pointRadius*2; 
    183183                height = height ? height : style.pointRadius*2; 
    184184                var resolution = this.getResolution(); 
    185                 node.style.left = (geometry.x/resolution-.5*width).toFixed(); 
    186                 node.style.top = (geometry.y/resolution-.5*height).toFixed(); 
     185                var xOffset = (style.graphicXOffset != undefined) ? 
     186                    style.graphicXOffset : -(0.5 * width); 
     187                var yOffset = (style.graphicYOffset != undefined) ? 
     188                    style.graphicYOffset : -(0.5 * height); 
     189                node.style.left = (geometry.x/resolution+xOffset).toFixed(); 
     190                node.style.top = (geometry.y/resolution+yOffset).toFixed(); 
    187191                node.style.width = width; 
    188192                node.style.height = height;     
    189193                 
  • lib/OpenLayers/Renderer/SVG.js

    old new  
    184184            if (style.externalGraphic) { 
    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; 
    191191                var _style = node._style; 
     
    206206                var height = style.graphicHeight || style.graphicWidth; 
    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); 
    215219                node.setAttributeNS("http://www.w3.org/1999/xlink", "href", style.externalGraphic); 
  • examples/vector-features.html

    old new  
    5959            // of the image will be ignored 
    6060            style_mark.graphicWidth = 24; 
    6161            style_mark.graphicHeight = 20; 
     62            style_mark.graphicXOffset = -(style_mark.graphicWidth/2); 
     63            style_mark.graphicYOffset = -style_mark.graphicHeight; 
    6264            style_mark.externalGraphic = "../img/marker.png"; 
    6365             
    6466            var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: layer_style});