⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kml.js

📁 用来在地图上做操作GIS,在地图上做标记
💻 JS
📖 第 1 页 / 共 2 页
字号:
/* 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/Format/XML.js * @requires OpenLayers/Feature/Vector.js * @requires OpenLayers/Geometry/Point.js * @requires OpenLayers/Geometry/LineString.js * @requires OpenLayers/Geometry/Polygon.js * @requires OpenLayers/Geometry/Collection.js * * Class: OpenLayers.Format.KML * Read/Wite KML. Create a new instance with the <OpenLayers.Format.KML> *     constructor.  *  * Inherits from: *  - <OpenLayers.Format.XML> */OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, {        /**     * APIProperty: kmlns     * {String} KML Namespace to use. Defaults to 2.0 namespace.     */    kmlns: "http://earth.google.com/kml/2.0",        /**      * APIProperty: placemarksDesc     * {String} Name of the placemarks.  Default is "No description available."     */    placemarksDesc: "No description available",        /**      * APIProperty: foldersName     * {String} Name of the folders.  Default is "OpenLayers export."     */    foldersName: "OpenLayers export",        /**      * APIProperty: foldersDesc     * {String} Description of the folders. Default is "Exported on [date]."     */    foldersDesc: "Exported on " + new Date(),        /**     * APIProperty: extractAttributes     * {Boolean} Extract attributes from KML.  Default is true.     */    extractAttributes: true,        /**     * Property: internalns     * {String} KML Namespace to use -- defaults to the namespace of the     *     Placemark node being parsed, but falls back to kmlns.      */    internalns: null,    /**     * Constructor: OpenLayers.Format.KML     * Create a new parser for KML.     *     * Parameters:     * options - {Object} An optional object whose properties will be set on     *     this instance.     */    initialize: function(options) {        // compile regular expressions once instead of every time they are used        this.regExes = {            trimSpace: (/^\s*|\s*$/g),            removeSpace: (/\s*/g),            splitSpace: (/\s+/),            trimComma: (/\s*,\s*/g)        };        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);    },    /**     * APIMethod: read     * Read data from a string, and return a list of features.      *      * Parameters:      * data - {String} or {DOMElement} data to read/parse.     *     * Returns:     * {Array(<OpenLayers.Feature.Vector>)} List of features.     */    read: function(data) {        if(typeof data == "string") {            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);        }        var featureNodes = this.getElementsByTagNameNS(data,                                                       '*',                                                       "Placemark");        var numFeatures = featureNodes.length;        var features = new Array(numFeatures);        for(var i=0; i<numFeatures; i++) {            var feature = this.parseFeature(featureNodes[i]);            if(feature) {                features[i] = feature;            } else {                throw "Bad Placemark: " + i;            }        }        return features;    },    /**     * Method: parseFeature     * This function is the core of the KML parsing code in OpenLayers.     *     It creates the geometries that are then attached to the returned     *     feature, and calls parseAttributes() to get attribute data out.     *     * Parameters:     * node - {<DOMElement>}     *     * Returns:     * {<OpenLayers.Feature.Vector>} A vector feature.     */    parseFeature: function(node) {        // only accept one geometry per feature - look for highest "order"        var order = ["MultiGeometry", "Polygon", "LineString", "Point"];        var type, nodeList, geometry, parser;        for(var i=0; i<order.length; ++i) {            type = order[i];            this.internalns = node.namespaceURI ?                     node.namespaceURI : this.kmlns;            nodeList = this.getElementsByTagNameNS(node,                                                    this.internalns, type);            if(nodeList.length > 0) {                // only deal with first geometry of this type                var parser = this.parseGeometry[type.toLowerCase()];                if(parser) {                    geometry = parser.apply(this, [nodeList[0]]);                } else {                    OpenLayers.Console.error("Unsupported geometry type: " +                                             type);                }                // stop looking for different geometry types                break;            }        }        // construct feature (optionally with attributes)        var attributes;        if(this.extractAttributes) {            attributes = this.parseAttributes(node);        }        var feature = new OpenLayers.Feature.Vector(geometry, attributes);        var fid = node.getAttribute("id");        if(fid != null) {            feature.fid = fid;        }        return feature;    },                /**     * Property: parseGeometry     * Properties of this object are the functions that parse geometries based     *     on their type.     */    parseGeometry: {                /**         * Method: parseGeometry.point         * Given a KML node representing a point geometry, create an OpenLayers         *     point geometry.         *         * Parameters:         * node - {DOMElement} A KML Point node.         *         * Returns:         * {<OpenLayers.Geometry.Point>} A point geometry.         */        point: function(node) {            var nodeList = this.getElementsByTagNameNS(node, this.internalns,                                                       "coordinates");            var coords = [];            if(nodeList.length > 0) {                var coordString = nodeList[0].firstChild.nodeValue;                coordString = coordString.replace(this.regExes.removeSpace, "");                coords = coordString.split(",");            }            var point = null;            if(coords.length > 1) {                // preserve third dimension                if(coords.length == 2) {                    coords[2] = null;                }                point = new OpenLayers.Geometry.Point(coords[0], coords[1],                                                      coords[2]);            } else {                throw "Bad coordinate string: " + coordString;            }            return point;        },                /**         * Method: parseGeometry.linestring         * Given a KML node representing a linestring geometry, create an         *     OpenLayers linestring geometry.         *         * Parameters:         * node - {DOMElement} A KML LineString node.         *         * Returns:         * {<OpenLayers.Geometry.LineString>} A linestring geometry.         */        linestring: function(node, ring) {            var nodeList = this.getElementsByTagNameNS(node, this.internalns,                                                       "coordinates");            var line = null;            if(nodeList.length > 0) {                var coordString = nodeList[0].firstChild.nodeValue;                coordString = coordString.replace(this.regExes.trimSpace,                                                  "");                coordString = coordString.replace(this.regExes.trimComma,                                                  ",");                var pointList = coordString.split(this.regExes.splitSpace);                var numPoints = pointList.length;                var points = new Array(numPoints);                var coords, numCoords;                for(var i=0; i<numPoints; ++i) {                    coords = pointList[i].split(",");                    numCoords = coords.length;                    if(numCoords > 1) {                        if(coords.length == 2) {                            coords[2] = null;                        }                        points[i] = new OpenLayers.Geometry.Point(coords[0],                                                                  coords[1],                                                                  coords[2]);                    } else {                        throw "Bad LineString point coordinates: " +                              pointList[i];                    }                }                if(numPoints) {                    if(ring) {                        line = new OpenLayers.Geometry.LinearRing(points);                    } else {                        line = new OpenLayers.Geometry.LineString(points);                    }                } else {                    throw "Bad LineString coordinates: " + coordString;                }            }            return line;        },                /**         * Method: parseGeometry.polygon         * Given a KML node representing a polygon geometry, create an         *     OpenLayers polygon geometry.         *         * Parameters:         * node - {DOMElement} A KML Polygon node.         *         * Returns:         * {<OpenLayers.Geometry.Polygon>} A polygon geometry.         */        polygon: function(node) {            var nodeList = this.getElementsByTagNameNS(node, this.internalns,                                                       "LinearRing");            var numRings = nodeList.length;            var components = new Array(numRings);            if(numRings > 0) {                // this assumes exterior ring first, inner rings after                var ring;                for(var i=0; i<nodeList.length; ++i) {                    ring = this.parseGeometry.linestring.apply(this,                                                        [nodeList[i], true]);                    if(ring) {                        components[i] = ring;                    } else {                        throw "Bad LinearRing geometry: " + i;                    }                }            }            return new OpenLayers.Geometry.Polygon(components);        },                /**         * Method: parseGeometry.multigeometry         * Given a KML node representing a multigeometry, create an         *     OpenLayers geometry collection.         *         * Parameters:         * node - {DOMElement} A KML MultiGeometry node.         *         * Returns:         * {<OpenLayers.Geometry.Collection>} A geometry collection.         */        multigeometry: function(node) {            var child, parser;            var parts = [];            var children = node.childNodes;            for(var i=0; i<children.length; ++i ) {                child = children[i];                if(child.nodeType == 1) {                    type = (child.prefix) ?                            child.nodeName.split(":")[1] :                            child.nodeName;                    var parser = this.parseGeometry[type.toLowerCase()];                    if(parser) {                        parts.push(parser.apply(this, [child]));                    }                }            }            return new OpenLayers.Geometry.Collection(parts);        }            },    /**     * Method: parseAttributes     *     * Parameters:     * node - {<DOMElement>}     *     * Returns:     * {Object} An attributes object.

⌨️ 快捷键说明

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