OpenLayers OpenLayers

Ticket #736: externalGraphic.2.diff

File externalGraphic.2.diff, 8.9 kB (added by ahocevar, 1 year ago)

Improved version which allows control over aspect ratio of the image; also added a description to vector-features.html

  • /mnt/d/eclipse/workspace/openlayers/examples/vector-features.html

    old new  
    77            border: 1px solid black; 
    88        } 
    99    </style> 
    10     <script src="../lib/OpenLayers.js"></script> 
     10    <script src="../lib/OpenLayers.js" type="text/javascript"></script> 
    1111    <script type="text/javascript"> 
    1212        <!-- 
    1313        var map; 
     
    2121            var style_blue = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']); 
    2222            style_blue.strokeColor = "blue";  
    2323            style_blue.fillColor = "blue";  
     24            style_blue.externalGraphic = "../img/marker.png"; 
     25             
     26            // each of the three lines below means the same, if only one of 
     27            // them is active: the image will have a size of 24px, and the 
     28            // aspect ratio will be kept 
     29            style_blue.pointRadius = 12; 
     30            //style_blue.graphicWidth = 24; 
     31            //style_blue.graphicHeight = 24; 
     32             
     33            style_blue.fillOpacity = 1; 
    2434            var style_green = { 
    2535                strokeColor: "#00FF00", 
    2636                strokeOpacity: 1, 
     
    2838                pointRadius: 6, 
    2939                pointerEvents: "visiblePainted" 
    3040            }; 
     41            var style_mark = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']); 
     42             
     43            // if graphicWidth and graphicHeight are both set, the aspect ratio 
     44            // of the image will be ignored 
     45            style_mark.graphicWidth = 24; 
     46            style_mark.graphicHeight = 20; 
     47             
     48            style_mark.externalGraphic = "../img/marker.png"; 
    3149             
    3250            var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry"); 
    3351             
     
    3452            // create a point feature 
    3553            var point = new OpenLayers.Geometry.Point(-111.04, 45.68); 
    3654            var pointFeature = new OpenLayers.Feature.Vector(point,null,style_blue); 
     55            var point2 = new OpenLayers.Geometry.Point(-105.04, 49.68); 
     56            var pointFeature2 = new OpenLayers.Feature.Vector(point2,null,style_green); 
     57            var point3 = new OpenLayers.Geometry.Point(-105.04, 49.68); 
     58            var pointFeature3 = new OpenLayers.Feature.Vector(point3,null,style_mark); 
    3759             
    3860            // create a line feature from a list of points 
    3961            var pointList = []; 
     
    6486             
    6587            map.addLayer(vectorLayer); 
    6688            map.setCenter(new OpenLayers.LonLat(point.x, point.y), 5); 
    67             vectorLayer.addFeatures([pointFeature, lineFeature, polygonFeature]); 
     89            vectorLayer.addFeatures([pointFeature, pointFeature3, pointFeature2, lineFeature, polygonFeature]); 
    6890        } 
    6991        // --> 
    7092    </script> 
     
    7597       in different styles, created 'manually', by constructing the entire style 
    7698       object, via 'copy', extending the default style object, and by  
    7799       inheriting the default style from the layer.</p> 
     100    <p>It also shows how to use external graphic files for point features 
     101       and how to set their size: If either graphicWidth or graphicHeight is set, 
     102       the aspect ratio of the image will be respected. If both graphicWidth and 
     103       graphicHeight are set, it will be ignored. Alternatively, if graphicWidth 
     104       and graphicHeight are omitted, pointRadius will be used to set the size 
     105       of the image, which will then be twice the value of pointRadius with the 
     106       original aspect ratio.</p> 
    78107  </body> 
    79108</html> 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Renderer/SVG.js

    old new  
    165165        options = options || node._options; 
    166166 
    167167        if (node._geometryClass == "OpenLayers.Geometry.Point") { 
    168             node.setAttributeNS(null, "r", style.pointRadius); 
     168            if (style.externalGraphic) { 
     169                // remove old node 
     170                var id = node.getAttributeNS(null, "id"); 
     171                var x = node.getAttributeNS(null, "cx"); 
     172                var y = node.getAttributeNS(null, "cy"); 
     173                var _featureId = node._featureId; 
     174                var _geometryClass = node._geometryClass; 
     175                var _style = node._style; 
     176                this.root.removeChild(node); 
     177                 
     178                // create new image node 
     179                var node = this.createNode("image", id); 
     180                node._featureId = _featureId; 
     181                node._geometryClass = _geometryClass; 
     182                node._style = _style; 
     183                this.root.appendChild(node); 
     184 
     185                // now style the new node 
     186                if (style.graphicWidth && style.graphicHeight) { 
     187                  node.setAttributeNS(null, "preserveAspectRatio", "none"); 
     188                } 
     189                var width = style.graphicWidth || style.graphicHeight; 
     190                var height = style.graphicHeight || style.graphicWidth; 
     191                width = width ? width : style.pointRadius*2; 
     192                height = height ? height : style.pointRadius*2; 
     193                 
     194                node.setAttributeNS(null, "x", x-(.5*width).toFixed()); 
     195                node.setAttributeNS(null, "y", -y-(.5*height).toFixed()); 
     196                node.setAttributeNS(null, "width", width); 
     197                node.setAttributeNS(null, "height", height); 
     198                node.setAttributeNS("http://www.w3.org/1999/xlink", "href", style.externalGraphic); 
     199                node.setAttributeNS(null, "transform", "scale(1,-1)"); 
     200                node.setAttributeNS(null, "style", "opacity: "+style.fillOpacity); 
     201            } else { 
     202                node.setAttributeNS(null, "r", style.pointRadius); 
     203            } 
    169204        } 
    170205         
    171206        if (options.isFilled) { 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Renderer/VML.js

    old new  
    129129        options = options || node._options; 
    130130         
    131131        if (node._geometryClass == "OpenLayers.Geometry.Point") { 
    132             this.drawCircle(node, geometry, style.pointRadius); 
     132            if (style.externalGraphic) { 
     133                // remove old node 
     134                var id = node.id; 
     135                var _featureId = node._featureId; 
     136                var _geometryClass = node._geometryClass; 
     137                var _style = node._style; 
     138                this.root.removeChild(node); 
     139                 
     140                // create new image node 
     141                var node = this.createNode("v:rect", id); 
     142                var fill = this.createNode("v:fill", id+"_image"); 
     143                node.appendChild(fill); 
     144                node._featureId = _featureId; 
     145                node._geometryClass = _geometryClass; 
     146                node._style = _style; 
     147                this.root.appendChild(node); 
     148                 
     149                fill.src = style.externalGraphic; 
     150                fill.type = "frame"; 
     151                node.style.flip = "y"; 
     152                 
     153                if (!(style.graphicWidth && style.graphicHeight)) { 
     154                  fill.aspect = "atmost"; 
     155                } 
     156                 
     157                // now style the new node 
     158                var width = style.graphicWidth || style.graphicHeight; 
     159                var height = style.graphicHeight || style.graphicWidth; 
     160                width = width ? width : style.pointRadius*2; 
     161                height = height ? height : style.pointRadius*2; 
     162                var resolution = this.getResolution(); 
     163                node.style.left = (geometry.x/resolution-.5*width).toFixed(); 
     164                node.style.top = (geometry.y/resolution-.5*height).toFixed(); 
     165                node.style.width = width; 
     166                node.style.height = height;     
     167                 
     168                // modify fill style for rect styling below 
     169                style.fillColor = "none"; 
     170                style.strokeColor = "none"; 
     171                          
     172            } else { 
     173                this.drawCircle(node, geometry, style.pointRadius); 
     174            } 
    133175        } 
    134176 
    135177      //fill 
     
    146188                fill = this.createNode('v:fill', node.id + "_fill"); 
    147189                node.appendChild(fill); 
    148190            } 
    149             fill.setAttribute("opacity", style.fillOpacity); 
     191            if (style.fillOpacity) { 
     192                fill.setAttribute("opacity", style.fillOpacity); 
     193            } 
    150194        } 
    151195 
    152196