OpenLayers OpenLayers

Ticket #893: patch-893-A3.diff

File patch-893-A3.diff, 8.0 kB (added by fredj, 1 year ago)

Fix the VML problems found in patch-893-A2

  • tests/Layer/test_Vector.html

    old new  
    158158    } 
    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 
    164164        // cannot be set 
     
    169169               format: 'image/png'}); 
    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); 
    177180 
     181        map.zoomToMaxExtent(); 
     182 
    178183        var customStyle1 = new Object({ 
    179184                externalGraphic: 'test.png', 
    180185                pointRadius: 10 
     
    197202                graphicWidth: 24, 
    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; 
    204216                layer.drawFeature(feature); 
     
    237249                t.eq(root.firstChild.getAttributeNS(null, 'style'), 
    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') { 
    242271                feature.style = customStyle1; 
     
    279308                t.eq(opacity, 
    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        } 
    284324    } 
  • 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  
    181181                var height = style.graphicHeight || style.graphicWidth; 
    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;     
    189195                 
  • 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  
    5858            // of the image will be ignored 
    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             
    6365            var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: layer_style});