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

📄 util.js

📁 用来在地图上做操作GIS,在地图上做标记
💻 JS
📖 第 1 页 / 共 2 页
字号:
 *  * Parameters: * to - {Object} * from - {Object} */OpenLayers.Util.applyDefaults = function (to, from) {    for (var key in from) {        if (to[key] == null) {            to[key] = from[key];        }    }};/** * Function: getParameterString *  * Parameters: * params - {Object} *  * Returns: * {String} A concatenation of the properties of an object in  *          http parameter notation.  *          (ex. <i>"key1=value1&key2=value2&key3=value3"</i>) *          If a parameter is actually a list, that parameter will then *          be set to a comma-seperated list of values (foo,bar) instead *          of being URL escaped (foo%3Abar).  */OpenLayers.Util.getParameterString = function(params) {    paramsArray = [];        for (var key in params) {      var value = params[key];      if ((value != null) && (typeof value != 'function')) {        var encodedValue;        if (typeof value == 'object' && value.constructor == Array) {          /* value is an array; encode items and separate with "," */          var encodedItemArray = [];          for (var itemIndex=0; itemIndex<value.length; itemIndex++) {            encodedItemArray.push(encodeURIComponent(value[itemIndex]));          }          encodedValue = encodedItemArray.join(",");        }        else {          /* value is a string; simply encode */          encodedValue = encodeURIComponent(value);        }        paramsArray.push(encodeURIComponent(key) + "=" + encodedValue);      }    }        return paramsArray.join("&");};/** * Property: ImgPath * {String} Default is ''. */OpenLayers.ImgPath = '';/**  * Function: getImagesLocation *  * Returns: * {String} The fully formatted image location string */OpenLayers.Util.getImagesLocation = function() {    return OpenLayers.ImgPath || (OpenLayers._getScriptLocation() + "img/");};/**  * Function: Try * Execute functions until one of them doesn't throw an error.  *     Capitalized because "try" is a reserved word in JavaScript. *     Taken directly from OpenLayers.Util.Try() *  * Parameters: * [*] - {Function} Any number of parameters may be passed to Try() *    It will attempt to execute each of them until one of them  *    successfully executes.  *    If none executes successfully, returns null. *  * Returns: * {*} The value returned by the first successfully executed function. */OpenLayers.Util.Try = function() {    var returnValue = null;    for (var i = 0; i < arguments.length; i++) {      var lambda = arguments[i];      try {        returnValue = lambda();        break;      } catch (e) {}    }    return returnValue;}/**  * Function: getNodes *  * These could/should be made namespace aware? *  * Parameters: * p - {} * tagName - {String} *  * Returns: * {Array} */OpenLayers.Util.getNodes=function(p, tagName) {    var nodes = OpenLayers.Util.Try(        function () {            return OpenLayers.Util._getNodes(p.documentElement.childNodes,                                            tagName);        },        function () {            return OpenLayers.Util._getNodes(p.childNodes, tagName);        }    );    return nodes;};/** * Function: _getNodes *  * Parameters: * nodes - {Array} * tagName - {String} *  * Returns: * {Array} */OpenLayers.Util._getNodes=function(nodes, tagName) {    var retArray = [];    for (var i=0;i<nodes.length;i++) {        if (nodes[i].nodeName==tagName) {            retArray.push(nodes[i]);        }    }    return retArray;};/** * Function: getTagText *  * Parameters: * parent - {} * item - {String} * index - {Integer} *  * Returns: * {String} */OpenLayers.Util.getTagText = function (parent, item, index) {    var result = OpenLayers.Util.getNodes(parent, item);    if (result && (result.length > 0))    {        if (!index) {            index=0;        }        if (result[index].childNodes.length > 1) {            return result.childNodes[1].nodeValue;         }        else if (result[index].childNodes.length == 1) {            return result[index].firstChild.nodeValue;         }    } else {         return "";     }};/** * Function: getXmlNodeValue *  * Parameters: * node - {XMLNode} *  * Returns: * {String} The text value of the given node, without breaking in firefox or IE */OpenLayers.Util.getXmlNodeValue = function(node) {    var val = null;    OpenLayers.Util.Try(         function() {            val = node.text;            if (!val)                val = node.textContent;            if (!val)                val = node.firstChild.nodeValue;        },         function() {            val = node.textContent;        });     return val;};/**  * Function: mouseLeft *  * Parameters: * evt - {Event} * div - {HTMLDivElement} *  * Returns: * {Boolean} */OpenLayers.Util.mouseLeft = function (evt, div) {    // start with the element to which the mouse has moved    var target = (evt.relatedTarget) ? evt.relatedTarget : evt.toElement;    // walk up the DOM tree.    while (target != div && target != null) {        target = target.parentNode;    }    // if the target we stop at isn't the div, then we've left the div.    return (target != div);};/** * Function: rad *  * Parameters: * x - {Float} *  * Returns: * {Float} */OpenLayers.Util.rad = function(x) {return x*Math.PI/180;};/** * Function: distVincenty *  * Parameters: * p1 - {Float} * p2 - {Float} *  * Returns: * {Float} */OpenLayers.Util.distVincenty=function(p1, p2) {    var a = 6378137, b = 6356752.3142,  f = 1/298.257223563;    var L = OpenLayers.Util.rad(p2.lon - p1.lon);    var U1 = Math.atan((1-f) * Math.tan(OpenLayers.Util.rad(p1.lat)));    var U2 = Math.atan((1-f) * Math.tan(OpenLayers.Util.rad(p2.lat)));    var sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);    var sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);    var lambda = L, lambdaP = 2*Math.PI;    var iterLimit = 20;    while (Math.abs(lambda-lambdaP) > 1e-12 && --iterLimit>0) {        var sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda);        var sinSigma = Math.sqrt((cosU2*sinLambda) * (cosU2*sinLambda) +        (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));        if (sinSigma==0) return 0;  // co-incident points        var cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;        var sigma = Math.atan2(sinSigma, cosSigma);        var alpha = Math.asin(cosU1 * cosU2 * sinLambda / sinSigma);        var cosSqAlpha = Math.cos(alpha) * Math.cos(alpha);        var cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;        var C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));        lambdaP = lambda;        lambda = L + (1-C) * f * Math.sin(alpha) *        (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));    }    if (iterLimit==0) return NaN  // formula failed to converge    var uSq = cosSqAlpha * (a*a - b*b) / (b*b);    var A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));    var B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));    var deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-        B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));    var s = b*A*(sigma-deltaSigma);    var d = s.toFixed(3)/1000; // round to 1mm precision    return d;};/** * Function: getParameters * Parse the parameters from a URL or from the current page itself into a  *     JavaScript Object. Note that parameter values with commas are separated *     out into an Array. *  * Parameters: * url - {String} Optional url used to extract the query string. *                If null, query string is taken from page location. *  * Returns: * {Object} An object of key/value pairs from the query string. */OpenLayers.Util.getParameters = function(url) {    //if no url specified, take it from the location bar    url = url || window.location.href    if(url == null) {        url = window.location.href;    }    //parse out parameters portion of url string    var paramsString = "";    if (OpenLayers.String.contains(url, '?')) {        var start = url.indexOf('?') + 1;        var end = OpenLayers.String.contains(url, "#") ?                    url.indexOf('#') : url.length;        paramsString = url.substring(start, end);    }            var parameters = {};    var pairs = paramsString.split(/[&;]/);    for(var i = 0; i < pairs.length; ++i) {        var keyValue = pairs[i].split('=');        if (keyValue[0]) {            var key = decodeURIComponent(keyValue[0]);            var value = keyValue[1] || ''; //empty string if no value            //decode individual values            value = value.split(",");            for(var j=0; j < value.length; j++) {                value[j] = decodeURIComponent(value[j]);            }            //if there's only one value, do not return as array                                if (value.length == 1) {                value = value[0];            }                                        parameters[key] = value;         }     }    return parameters;};/** * Function: getArgs * Deprecated - Will be removed in 3.0.  *     Please use instead OpenLayers.Util.getParameters *  * Parameters: * url - {String} Optional url used to extract the query string. *                If null, query string is taken from page location. *  * Returns: * {Object} An object of key/value pairs from the query string. */OpenLayers.Util.getArgs = function(url) {    var err = "The getArgs() function is deprecated and will be removed " +            "with the 3.0 version of OpenLayers. Please instead use " +            "OpenLayers.Util.getParameters().";    OpenLayers.Console.warn(err);    return OpenLayers.Util.getParameters(url);};/** * Property: lastSeqID * {Integer} The ever-incrementing count variable. *           Used for generating unique ids. */OpenLayers.Util.lastSeqID = 0;/** * Function: createUniqueID *  * Parameters: * prefix {String} String to prefix unique id.  *                 If null, default is "id_" *  * Returns: * {String} A unique id string, built on the passed in prefix */OpenLayers.Util.createUniqueID = function(prefix) {    if (prefix == null) {        prefix = "id_";    }    OpenLayers.Util.lastSeqID += 1;     return prefix + OpenLayers.Util.lastSeqID;        };/** * Constant: INCHES_PER_UNIT * {Object} Constant inches per unit -- borrowed from MapServer mapscale.c */OpenLayers.INCHES_PER_UNIT = {     'inches': 1.0,    'ft': 12.0,    'mi': 63360.0,    'm': 39.3701,    'km': 39370.1,    'dd': 4374754};OpenLayers.INCHES_PER_UNIT["in"]= OpenLayers.INCHES_PER_UNIT.inches;OpenLayers.INCHES_PER_UNIT["degrees"] = OpenLayers.INCHES_PER_UNIT.dd;/**  * Constant: DOTS_PER_INCH * {Integer} 72 (A sensible default) */OpenLayers.DOTS_PER_INCH = 72;/** * Function: normalzeScale *  * Parameters: * scale - {float} *  * Returns: * {Float} A normalized scale value, in 1 / X format.  *         This means that if a value less than one ( already 1/x) is passed *         in, it just returns scale directly. Otherwise, it returns  *         1 / scale */OpenLayers.Util.normalizeScale = function (scale) {    var normScale = (scale > 1.0) ? (1.0 / scale)                                   : scale;    return normScale;};/** * Function: getResolutionFromScale *  * Parameters: * scale - {Float} * units - {String} Index into OpenLayers.INCHES_PER_UNIT hashtable. *                  Default is degrees *  * Returns: * {Float} The corresponding resolution given passed-in scale and unit  *         parameters. */OpenLayers.Util.getResolutionFromScale = function (scale, units) {    if (units == null) {        units = "degrees";    }    var normScale = OpenLayers.Util.normalizeScale(scale);    var resolution = 1 / (normScale * OpenLayers.INCHES_PER_UNIT[units]                                    * OpenLayers.DOTS_PER_INCH);    return resolution;};/** * Function: getScaleFromResolution *  * Parameters: * resolution - {Float} * units - {String} Index into OpenLayers.INCHES_PER_UNIT hashtable. *                  Default is degrees *  * Returns: * {Float} The corresponding scale given passed-in resolution and unit  *         parameters. */OpenLayers.Util.getScaleFromResolution = function (resolution, units) {    if (units == null) {        units = "degrees";    }    var scale = resolution * OpenLayers.INCHES_PER_UNIT[units] *                    OpenLayers.DOTS_PER_INCH;    return scale;};/** * Function: safeStopPropagation * Deprecated. *  * This function has been deprecated. Please use directly  *     OpenLayers.Event.stop() passing 'true' as the 2nd  *     argument (preventDefault) *  * Safely stop the propagation of an event *without* preventing *   the default browser action from occurring. *  * Parameter: * evt - {Event} */OpenLayers.Util.safeStopPropagation = function(evt) {    OpenLayers.Event.stop(evt, true);};/** * Function: pagePositon * Calculates the position of an element on the page.  * * Parameters: * forElement - {DOMElement} *  * Returns:

⌨️ 快捷键说明

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