[OpenLayers-Dev] vector labels

Piebe de Vries piebe.de.vries at geodan.nl
Mon Jun 30 05:10:31 EDT 2008


An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-dev/attachments/20080630/4bd43ba7/attachment.html
-------------- next part --------------
Index: Elements.js
===================================================================
--- Elements.js	(revision 6549)
+++ Elements.js	(working copy)
@@ -151,6 +151,9 @@
 	            this.root.appendChild(node); 
 	        }
             this.postDraw(node);
+			
+	        // draw potential label
+			this.drawLabel(node, geometry, style);
         } else {
             node = OpenLayers.Util.getElement(geometry.id);
             if (node) {
@@ -210,6 +213,32 @@
         return this.setStyle(node, style, options, geometry);
     },
     
+	   /**
+     * Method: drawLabel
+     * Draws a potential label if set in style
+     * 
+     * Parameters:
+     * node - {DOMElement}
+     * geometry - {<OpenLayers.Geometry>}
+     * style - {Object}
+     */
+    drawLabel: function(node, geometry, style) {
+        if (labelsVisible) // HACK
+		{
+			if (style.label) 
+			{
+				this.drawText(node, style, geometry.getBounds().getCenterPixel());
+			}
+		}
+		else
+		{
+			if (style.label) 
+			{
+				this.removeText(node, style)
+			}			
+		}
+    },
+	
     /**
      * Method: postDraw
      * Things that have do be done after the geometry node is appended
Index: SVG.js
===================================================================
--- SVG.js	(revision 6549)
+++ SVG.js	(working copy)
@@ -464,6 +464,50 @@
             node.setAttributeNS(null, "d", "");
         }    
     },
+    
+    /**
+     * Method: drawText
+     * This method is only called by the renderer itself.
+     * 
+     * Parameters: 
+     * node - {DOMElement}
+     * style -
+     * location - {<OpenLayers.Geometry.Point>}
+     */
+    drawText: function(node, style, location) {
+        var resolution = this.getResolution();
+        
+        var x = (location.x / resolution + this.left);
+        var y = (location.y / resolution - this.top);
+        
+        var label = this.nodeFactory("label" + node.id, "text");
+        label.setAttributeNS(null, "x", x);
+        label.setAttributeNS(null, "y", -y);
+        
+        if (style.fontColor) {
+            label.setAttributeNS(null, "fill", style.fontColor);
+        }
+        if (style.fontFamily) {
+            label.setAttributeNS(null, "font-family", style.fontFamily);
+        }
+        if (style.fontSize) {
+            label.setAttributeNS(null, "font-size", style.fontSize);
+        }
+        if (style.fontWeight) {
+            label.setAttributeNS(null, "font-weight", style.fontWeight);
+        }
+        
+        /* No need to scale Text... */
+        label.setAttributeNS(null, "transform", "scale(1, -1)");
+        
+        // prevent label duplication
+        if (label.firstChild) {
+            label.removeChild(label.firstChild);
+        }
+        var data = document.createTextNode(style.label);
+        label.appendChild(data);
+        node.parentNode.appendChild(label);
+    },
 
     /** 
      * Method: getComponentString
Index: VML.js
===================================================================
--- VML.js	(revision 6549)
+++ VML.js	(working copy)
@@ -552,6 +552,52 @@
 
         node.path = path.join("");
     },
+    /**
+     * Method: drawText
+     * This method is only called by the renderer itself and must only
+     *    be called after the node is append to renderer.root
+     * 
+     * Parameters: 
+     * node - {DOMElement}
+     * style -
+     * location - {<OpenLayers.Geometry.Point>}
+     */
+    drawText: function(node, style, location) {
+        
+        var label = this.nodeFactory("label" + node.id, "v:oval");
+        
+        var resolution = this.getResolution();
+    
+        label.style.left = (location.x /resolution).toFixed() ;
+        label.style.top = (location.y /resolution).toFixed();
 
+        var textbox = this.createNode("v:textbox");
+        
+        var span = document.createElement('span');
+        if (style.fontColor) {
+            span.style.color = style.fontColor;
+        }
+        if (style.fontFamily) {
+            span.style.fontFamily = style.fontFamily;
+        }
+        if (style.fontSize) {
+            span.style.fontSize = style.fontSize;
+        }
+        if (style.fontWeight) {
+            span.style.fontWeight = style.fontWeight;
+        }
+
+        span.innerHTML = style.label;
+        
+        textbox.appendChild(span);
+        
+        // prevent label duplication
+        if (label.firstChild) {
+            label.removeChild(label.firstChild);
+        }
+        
+        label.appendChild(textbox);
+        node.parentNode.appendChild(label);
+    },
     CLASS_NAME: "OpenLayers.Renderer.VML"
 });


More information about the Dev mailing list