vml.js

来自「用来在地图上做操作GIS,在地图上做标记」· JavaScript 代码 · 共 569 行 · 第 1/2 页

JS
569
字号
/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license. * See http://svn.openlayers.org/trunk/openlayers/release-license.txt * for the full text of the license. *//** * @requires OpenLayers/Renderer/Elements.js * * Class: OpenLayers.Renderer.VML * Render vector features in browsers with VML capability.  Construct a new * VML renderer with the <OpenLayers.Renderer.VML> constructor. *  * Note that for all calculations in this class, we use toFixed() to round a  * float value to an integer. This is done because it seems that VML doesn't  * support float values. * * Inherits from: *  - <OpenLayers.Renderer.Elements> */OpenLayers.Renderer.VML = OpenLayers.Class(OpenLayers.Renderer.Elements, {    /**     * Property: xmlns     * {String} XML Namespace URN     */    xmlns: "urn:schemas-microsoft-com:vml",    /**     * Constructor: OpenLayers.Renderer.VML     * Create a new VML renderer.     *     * Parameters:     * containerID - {String} The id for the element that contains the renderer     */    initialize: function(containerID) {        if (!this.supported()) {             return;         }         document.namespaces.add("v", "urn:schemas-microsoft-com:vml");        var style = document.createStyleSheet();        style.addRule('v\\:*', "behavior: url(#default#VML); " +                               "position: relative; display: inline-block;");        OpenLayers.Renderer.Elements.prototype.initialize.apply(this,                                                                 arguments);    },    /**     * APIMethod: destroy     * Deconstruct the renderer.     */    destroy: function() {        OpenLayers.Renderer.Elements.prototype.destroy.apply(this, arguments);    },    /**     * APIMethod: supported     * Determine whether a browser supports this renderer.     *     * Returns:     * {Boolean} The browser supports the VML renderer     */    supported: function() {        var supported = document.namespaces;        return supported;    },        /**     * Method: setExtent     * Set the renderer's extent     *     * Parameters:     * extent - {<OpenLayers.Bounds>}     */    setExtent: function(extent) {        OpenLayers.Renderer.Elements.prototype.setExtent.apply(this,                                                                arguments);        var resolution = this.getResolution();            var org = extent.left/resolution + " " +                     extent.top/resolution;        this.root.setAttribute("coordorigin", org);        var size = extent.getWidth()/resolution + " " +                     -extent.getHeight()/resolution;        this.root.setAttribute("coordsize", size);    },    /**     * Method: setSize     * Set the size of the drawing surface     *     * Parameters:     * size - {<OpenLayers.Size>} the size of the drawing surface     */    setSize: function(size) {        OpenLayers.Renderer.prototype.setSize.apply(this, arguments);        this.rendererRoot.style.width = this.size.w;        this.rendererRoot.style.height = this.size.h;        this.root.style.width = "100%";        this.root.style.height = "100%";    },    /**     * Method: getNodeType     * Get the noode type for a geometry     *     * Parameters:     * geometry - {<OpenLayers.Geometry>}     *     * Returns:     * {String} The corresponding node type for the specified geometry     */    getNodeType: function(geometry) {        var nodeType = null;        switch (geometry.CLASS_NAME) {            case "OpenLayers.Geometry.Point":                nodeType = "v:oval";                break;            case "OpenLayers.Geometry.Rectangle":                nodeType = "v:rect";                break;            case "OpenLayers.Geometry.LineString":            case "OpenLayers.Geometry.LinearRing":            case "OpenLayers.Geometry.Polygon":            case "OpenLayers.Geometry.Curve":            case "OpenLayers.Geometry.Surface":                nodeType = "v:shape";                break;            default:                break;        }        return nodeType;    },    /**     * Method: setStyle     * Use to set all the style attributes to a VML node.     *     * Parameters:     * node - {DOMElement}      * style - {Object}     * options - {Object}      * isFilled - {Boolean}      * isStroked - {Boolean}     * geometry - {<OpenLayers.Geometry>}     */    setStyle: function(node, style, options, geometry) {        style = style  || node._style;        options = options || node._options;                if (node._geometryClass == "OpenLayers.Geometry.Point") {            if (style.externalGraphic) {                // remove old node                var id = node.id;                var _featureId = node._featureId;                var _geometryClass = node._geometryClass;                var _style = node._style;                this.root.removeChild(node);                                // create new image node                var node = this.createNode("v:rect", id);                var fill = this.createNode("v:fill", id+"_image");                node.appendChild(fill);                node._featureId = _featureId;                node._geometryClass = _geometryClass;                node._style = _style;                this.root.appendChild(node);                                fill.src = style.externalGraphic;                fill.type = "frame";                node.style.flip = "y";                                if (!(style.graphicWidth && style.graphicHeight)) {                  fill.aspect = "atmost";                }                                // now style the new node                var width = style.graphicWidth || style.graphicHeight;                var height = style.graphicHeight || style.graphicWidth;                width = width ? width : style.pointRadius*2;                height = height ? height : style.pointRadius*2;                var resolution = this.getResolution();                var xOffset = (style.graphicXOffset != undefined) ?                    style.graphicXOffset : -(0.5 * width);                var yOffset = (style.graphicYOffset != undefined) ?                    style.graphicYOffset : -(0.5 * height);                                node.style.left = ((geometry.x/resolution)+xOffset).toFixed();                node.style.top = ((geometry.y/resolution)-(yOffset+height)).toFixed();                node.style.width = width;                node.style.height = height;                                    // modify fill style for rect styling below                style.fillColor = "none";                style.strokeColor = "none";                                     } else {                this.drawCircle(node, geometry, style.pointRadius);            }        }      //fill        var fillColor = (options.isFilled) ? style.fillColor : "none";        node.setAttribute("fillcolor", fillColor);        var fills = node.getElementsByTagName("fill");        var fill = (fills.length == 0) ? null : fills[0];        if (!options.isFilled) {            if (fill) {                node.removeChild(fill);            }        } else {            if (!fill) {                fill = this.createNode('v:fill', node.id + "_fill");                node.appendChild(fill);            }            // if graphicOpacity is set use it in priority for external graphic            if (node._geometryClass == "OpenLayers.Geometry.Point" &&                style.externalGraphic &&                style.graphicOpacity) {                fill.setAttribute("opacity", style.graphicOpacity);            } else if (style.fillOpacity) {                fill.setAttribute("opacity", style.fillOpacity);            }        }      //stroke        var strokeColor = (options.isStroked) ? style.strokeColor : "none";        node.setAttribute("strokecolor", strokeColor);        node.setAttribute("strokeweight", style.strokeWidth);        var strokes = node.getElementsByTagName("stroke");        var stroke = (strokes.length == 0) ? null : strokes[0];        if (!options.isStroked) {            if (stroke) {                node.removeChild(stroke);            }        } else {            if (!stroke) {                stroke = this.createNode('v:stroke', node.id + "_stroke");                node.appendChild(stroke);            }            stroke.setAttribute("opacity", style.strokeOpacity);            stroke.setAttribute("endcap", !style.strokeLinecap || style.strokeLinecap == 'butt' ? 'flat' : style.strokeLinecap);        }                if (style.cursor) {            node.style.cursor = style.cursor;        }    },    /**     * Method: setNodeDimension     * Get the geometry's bounds, convert it to our vml coordinate system,      * then set the node's position, size, and local coordinate system.     *        * Parameters:     * node - {DOMElement}     * geometry - {<OpenLayers.Geometry>}     */    setNodeDimension: function(node, geometry) {        var bbox = geometry.getBounds();        if(bbox) {            var resolution = this.getResolution();                    var scaledBox =                 new OpenLayers.Bounds((bbox.left/resolution).toFixed(),                                      (bbox.bottom/resolution).toFixed(),                                      (bbox.right/resolution).toFixed(),                                      (bbox.top/resolution).toFixed());                        // Set the internal coordinate system to draw the path            node.style.left = scaledBox.left;            node.style.top = scaledBox.top;            node.style.width = scaledBox.getWidth();            node.style.height = scaledBox.getHeight();                node.coordorigin = scaledBox.left + " " + scaledBox.top;            node.coordsize = scaledBox.getWidth()+ " " + scaledBox.getHeight();        }    },

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?