--- a/labs/openlayers/lib/OpenLayers/Util.js +++ b/labs/openlayers/lib/OpenLayers/Util.js @@ -1,1 +1,1798 @@ - +/* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the Clear BSD license. + * See http://svn.openlayers.org/trunk/openlayers/license.txt for the + * full text of the license. */ + +/** + * @requires OpenLayers/Console.js + */ + +/** + * Namespace: Util + */ +OpenLayers.Util = {}; + +/** + * Function: getElement + * This is the old $() from prototype + */ +OpenLayers.Util.getElement = function() { + var elements = []; + + for (var i=0, len=arguments.length; i<len; i++) { + var element = arguments[i]; + if (typeof element == 'string') { + element = document.getElementById(element); + } + if (arguments.length == 1) { + return element; + } + elements.push(element); + } + return elements; +}; + +/** + * Function: isElement + * A cross-browser implementation of "e instanceof Element". + * + * Parameters: + * o - {Object} The object to test. + * + * Returns: + * {Boolean} + */ +OpenLayers.Util.isElement = function(o) { + return !!(o && o.nodeType === 1); +}; + +/** + * Maintain existing definition of $. + */ +if(typeof window.$ === "undefined") { + window.$ = OpenLayers.Util.getElement; +} + +/** + * APIFunction: extend + * Copy all properties of a source object to a destination object. Modifies + * the passed in destination object. Any properties on the source object + * that are set to undefined will not be (re)set on the destination object. + * + * Parameters: + * destination - {Object} The object that will be modified + * source - {Object} The object with properties to be set on the destination + * + * Returns: + * {Object} The destination object. + */ +OpenLayers.Util.extend = function(destination, source) { + destination = destination || {}; + if(source) { + for(var property in source) { + var value = source[property]; + if(value !== undefined) { + destination[property] = value; + } + } + + /** + * IE doesn't include the toString property when iterating over an object's + * properties with the for(property in object) syntax. Explicitly check if + * the source has its own toString property. + */ + + /* + * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative + * prototype object" when calling hawOwnProperty if the source object + * is an instance of window.Event. + */ + + var sourceIsEvt = typeof window.Event == "function" + && source instanceof window.Event; + + if(!sourceIsEvt + && source.hasOwnProperty && source.hasOwnProperty('toString')) { + destination.toString = source.toString; + } + } + return destination; +}; + + +/** + * Function: removeItem + * Remove an object from an array. Iterates through the array + * to find the item, then removes it. + * + * Parameters: + * array - {Array} + * item - {Object} + * + * Return + * {Array} A reference to the array + */ +OpenLayers.Util.removeItem = function(array, item) { + for(var i = array.length - 1; i >= 0; i--) { + if(array[i] == item) { + array.splice(i,1); + //break;more than once?? + } + } + return array; +}; + +/** + * Function: clearArray + * *Deprecated*. This function will disappear in 3.0. + * Please use "array.length = 0" instead. + * + * Parameters: + * array - {Array} + */ +OpenLayers.Util.clearArray = function(array) { + OpenLayers.Console.warn( + OpenLayers.i18n( + "methodDeprecated", {'newMethod': 'array = []'} + ) + ); + array.length = 0; +}; + +/** + * Function: indexOf + * Seems to exist already in FF, but not in MOZ. + * + * Parameters: + * array - {Array} + * obj - {Object} + * + * Returns: + * {Integer} The index at, which the first object was found in the array. + * If not found, returns -1. + */ +OpenLayers.Util.indexOf = function(array, obj) { + // use the build-in function if available. + if (typeof array.indexOf == "function") { + return array.indexOf(obj); + } else { + for (var i = 0, len = array.length; i < len; i++) { + if (array[i] == obj) { + return i; + } + } + return -1; + } +}; + + + +/** + * Function: modifyDOMElement + * + * Modifies many properties of a DOM element all at once. Passing in + * null to an individual parameter will avoid setting the attribute. + * + * Parameters: + * id - {String} The element id attribute to set. + * px - {<OpenLayers.Pixel>} The left and top style position. + * sz - {<OpenLayers.Size>} The width and height style attributes. + * position - {String} The position attribute. eg: absolute, + * relative, etc. + * border - {String} The style.border attribute. eg: + * solid black 2px + * overflow - {String} The style.overview attribute. + * opacity - {Float} Fractional value (0.0 - 1.0) + */ +OpenLayers.Util.modifyDOMElement = function(element, id, px, sz, position, + border, overflow, opacity) { + + if (id) { + element.id = id; + } + if (px) { + element.style.left = px.x + "px"; + element.style.top = px.y + "px"; + } + if (sz) { + element.style.width = sz.w + "px"; + element.style.height = sz.h + "px"; + } + if (position) { + element.style.position = position; + } + if (border) { + element.style.border = border; + } + if (overflow) { + element.style.overflow = overflow; + } + if (parseFloat(opacity) >= 0.0 && parseFloat(opacity) < 1.0) { + element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')'; + element.style.opacity = opacity; + } else if (parseFloat(opacity) == 1.0) { + element.style.filter = ''; + element.style.opacity = ''; + } +}; + +/** + * Function: createDiv + * Creates a new div and optionally set some standard attributes. + * Null may be passed to each parameter if you do not wish to + * set a particular attribute. + * Note - zIndex is NOT set on the resulting div. + * + * Parameters: + * id - {String} An identifier for this element. If no id is + * passed an identifier will be created + * automatically. + * px - {<OpenLayers.Pixel>} The element left and top position. + * sz - {<OpenLayers.Size>} The element width and height. + * imgURL - {String} A url pointing to an image to use as a + * background image. + * position - {String} The style.position value. eg: absolute, + * relative etc. + * border - {String} The the style.border value. + * eg: 2px solid black + * overflow - {String} The style.overflow value. Eg. hidden + * opacity - {Float} Fractional value (0.0 - 1.0) + * + * Returns: + * {DOMElement} A DOM Div created with the specified attributes. + */ +OpenLayers.Util.createDiv = function(id, px, sz, imgURL, position, + border, overflow, opacity) { + + var dom = document.createElement('div'); + + if (imgURL) { + dom.style.backgroundImage = 'url(' + imgURL + ')'; + } + + //set generic properties + if (!id) { + id = OpenLayers.Util.createUniqueID("OpenLayersDiv"); + } + if (!position) { + position = "absolute"; + } + OpenLayers.Util.modifyDOMElement(dom, id, px, sz, position, + border, overflow, opacity); + + return dom; +}; + +/** + * Function: createImage + * Creates an img element with specific attribute values. + * + * Parameters: + * id - {String} The id field for the img. If none assigned one will be + * automatically generated. + * px - {<OpenLayers.Pixel>} The left and top positions. + * sz - {<OpenLayers.Size>} The style.width and style.height values. + * imgURL - {String} The url to use as the image source. + * position - {String} The style.position value. + * border - {String} The border to place around the image. + * opacity - {Float} Fractional value (0.0 - 1.0) + * delayDisplay - {Boolean} If true waits until the image has been + * loaded. + * + * Returns: + * {DOMElement} A DOM Image created with the specified attributes. + */ +OpenLayers.Util.createImage = function(id, px, sz, imgURL, position, border, + opacity, delayDisplay) { + + var image = document.createElement("img"); + + //set generic properties + if (!id) { + id = OpenLayers.Util.createUniqueID("OpenLayersDiv"); + } + if (!position) { + position = "relative"; + } + OpenLayers.Util.modifyDOMElement(image, id, px, sz, position, + border, null, opacity); + + if(delayDisplay) { + image.style.display = "none"; + OpenLayers.Event.observe(image, "load", + OpenLayers.Function.bind(OpenLayers.Util.onImageLoad, image)); + OpenLayers.Event.observe(image, "error", + OpenLayers.Function.bind(OpenLayers.Util.onImageLoadError, image)); + + } + + //set special properties + image.style.alt = id; + image.galleryImg = "no"; + if (imgURL) { + image.src = imgURL; + } + + + + return image; +}; + +/** + * Function: setOpacity + * *Deprecated*. This function has been deprecated. Instead, please use + * <OpenLayers.Util.modifyDOMElement> + * or + * <OpenLayers.Util.modifyAlphaImageDiv> + * + * Set the opacity of a DOM Element + * Note that for this function to work in IE, elements must "have layout" + * according to: + * http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/haslayout.asp + * + * Parameters: + * element - {DOMElement} Set the opacity on this DOM element + * opacity - {Float} Opacity value (0.0 - 1.0) + */ +OpenLayers.Util.setOpacity = function(element, opacity) { + OpenLayers.Util.modifyDOMElement(element, null, null, null, + null, null, null, opacity); +}; + +/** + * Function: onImageLoad + * Bound to image load events. For all images created with <createImage> or + * <createAlphaImageDiv>, this function will be bound to the load event. + */ +OpenLayers.Util.onImageLoad = function() { + // The complex check here is to solve issues described in #480. + // Every time a map view changes, it increments the 'viewRequestID' + // property. As the requests for the images for the new map view are sent + // out, they are tagged with this unique viewRequestID. + // + // If an image has no viewRequestID property set, we display it regardless, + // but if it does have a viewRequestID property, we check that it matches + // the viewRequestID set on the map. + // + // If the viewRequestID on the map has changed, that means that the user + // has changed the map view since this specific request was sent out, and + // therefore this tile does not need to be displayed (so we do not execute + // this code that turns its display on). + // + if (!this.viewRequestID || + (this.map && this.viewRequestID == this.map.viewRequestID)) { + this.style.display = ""; + } + OpenLayers.Element.removeClass(this, "olImageLoadError"); +}; + +/** + * Property: IMAGE_RELOAD_ATTEMPTS + * {Integer} How many times should we try to reload an image before giving up? + * Default is 0 + */ +OpenLayers.IMAGE_RELOAD_ATTEMPTS = 0; + +/** + * Function: onImageLoadError + */ +OpenLayers.Util.onImageLoadError = function() { + this._attempts = (this._attempts) ? (this._attempts + 1) : 1; + if (this._attempts <= OpenLayers.IMAGE_RELOAD_ATTEMPTS) { + var urls = this.urls; + if (urls && urls instanceof Array && urls.length > 1){ + var src = this.src.toString(); + var current_url, k; + for (k = 0; current_url = urls[k]; k++){ + if(src.indexOf(current_url) != -1){ + break; + } + } + var guess = Math.floor(urls.length * Math.random()); + var new_url = urls[guess]; + k = 0; + while(new_url == current_url && k++ < 4){ + guess = Math.floor(urls.length * Math.random()); + new_url = urls[guess]; + } + this.src = src.replace(current_url, new_url); + } else { + this.src = this.src; + } + } else { + OpenLayers.Element.addClass(this, "olImageLoadError"); + } + this.style.display = ""; +}; + +/** + * Property: alphaHackNeeded + * {Boolean} true if the png alpha hack is necessary and possible, false otherwise. + */ +OpenLayers.Util.alphaHackNeeded = null; + +/** + * Function: alphaHack + * Checks whether it's necessary (and possible) to use the png alpha + * hack which allows alpha transparency for png images under Internet + * Explorer. + * + * Returns: + * {Boolean} true if the png alpha hack is necessary and possible, false otherwise. + */ +OpenLayers.Util.alphaHack = function() { + if (OpenLayers.Util.alphaHackNeeded == null) { + var arVersion = navigator.appVersion.split("MSIE"); + var version = parseFloat(arVersion[1]); + var filter = false; + + // IEs4Lin dies when trying to access document.body.filters, because + // the property is there, but requires a DLL that can't be provided. This + // means that we need to wrap this in a try/catch so that this can + // continue. + + try { + filter = !!(document.body.filters); + } catch (e) {} + + OpenLayers.Util.alphaHackNeeded = (filter && + (version >= 5.5) && (version < 7)); + } + return OpenLayers.Util.alphaHackNeeded; +}; + +/** + * Function: modifyAlphaImageDiv + * + * div - {DOMElement} Div containing Alpha-adjusted Image + * id - {String} + * px - {<OpenLayers.Pixel>} + * sz - {<OpenLayers.Size>} + * imgURL - {String} + * position - {String} + * border - {String} + * sizing {String} 'crop', 'scale', or 'image'. Default is "scale" + * opacity - {Float} Fractional value (0.0 - 1.0) + */ +OpenLayers.Util.modifyAlphaImageDiv = function(div, id, px, sz, imgURL, + position, border, sizing, + opacity) { + + OpenLayers.Util.modifyDOMElement(div, id, px, sz, position, + null, null, opacity); + + var img = div.childNodes[0]; + + if (imgURL) { + img.src = imgURL; + } + OpenLayers.Util.modifyDOMElement(img, div.id + "_innerImage", null, sz, + "relative", border); + + if (OpenLayers.Util.alphaHack()) { + if(div.style.display != "none") { + div.style.display = "inline-block"; + } + if (sizing == null) { + sizing = "scale"; + } + + div.style.filter = "progid:DXImageTransform.Microsoft" + + ".AlphaImageLoader(src='" + img.src + "', " + + "sizingMethod='" + sizing + "')"; + if (parseFloat(div.style.opacity) >= 0.0 && + parseFloat(div.style.opacity) < 1.0) { + div.style.filter += " alpha(opacity=" + div.style.opacity * 100 + ")"; + } + + img.style.filter = "alpha(opacity=0)"; + } +}; + +/** + * Function: createAlphaImageDiv + * + * id - {String} + * px - {<OpenLayers.Pixel>} + * sz - {<OpenLayers.Size>} + * imgURL - {String} + * position - {String} + * border - {String} + * sizing - {String} 'crop', 'scale', or 'image'. Default is "scale" + * opacity - {Float} Fractional value (0.0 - 1.0) + * delayDisplay - {Boolean} If true waits until the image has been + * loaded. + * + * Returns: + * {DOMElement} A DOM Div created with a DOM Image inside it. If the hack is + * needed for transparency in IE, it is added. + */ +OpenLayers.Util.createAlphaImageDiv = function(id, px, sz, imgURL, + position, border, sizing, + opacity, delayDisplay) { + + var div = OpenLayers.Util.createDiv(); + var img = OpenLayers.Util.createImage(null, null, null, null, null, null, + null, false); + div.appendChild(img); + + if (delayDisplay) { + img.style.display = "none"; + OpenLayers.Event.observe(img, "load", + OpenLayers.Function.bind(OpenLayers.Util.onImageLoad, div)); + OpenLayers.Event.observe(img, "error", + OpenLayers.Function.bind(OpenLayers.Util.onImageLoadError, div)); + } + + OpenLayers.Util.modifyAlphaImageDiv(div, id, px, sz, imgURL, position, + border, sizing, opacity); + + return div; +}; + + +/** + * Function: upperCaseObject + * Creates a new hashtable and copies over all the keys from the + * passed-in object, but storing them under an uppercased + * version of the key at which they were stored. + * + * Parameters: + * object - {Object} + * + * Returns: + * {Object} A new Object with all the same keys but uppercased + */ +OpenLayers.Util.upperCaseObject = function (object) { + var uObject = {}; + for (var key in object) { + uObject[key.toUpperCase()] = object[key]; + } + return uObject; +}; + +/** + * Function: applyDefaults + * Takes an object and copies any properties that don't exist from + * another properties, by analogy with OpenLayers.Util.extend() from + * Prototype.js. + * + * Parameters: + * to - {Object} The destination object. + * from - {Object} The source object. Any properties of this object that + * are undefined in the to object will be set on the to object. + * + * Returns: + * {Object} A reference to the to object. Note that the to argument is modified + * in place and returned by this function. + */ +OpenLayers.Util.applyDefaults = function (to, from) { + to = to || {}; + /* + * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative + * prototype object" when calling hawOwnProperty if the source object is an + * instance of window.Event. + */ + var fromIsEvt = typeof window.Event == "function" + && from instanceof window.Event; + + for (var key in from) { + if (to[key] === undefined || + (!fromIsEvt && from.hasOwnProperty + && from.hasOwnProperty(key) && !to.hasOwnProperty(key))) { + to[key] = from[key]; + } + } + /** + * IE doesn't include the toString property when iterating over an object's + * properties with the for(property in object) syntax. Explicitly check if + * the source has its own toString property. + */ + if(!fromIsEvt && from && from.hasOwnProperty + && from.hasOwnProperty('toString') && !to.hasOwnProperty('toString')) { + to.toString = from.toString; + } + + return to; +}; + +/** + * 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) { + var 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 = []; + var item; + for (var itemIndex=0, len=value.length; itemIndex<len; itemIndex++) { + item = value[itemIndex]; + encodedItemArray.push(encodeURIComponent( + (item === null || item === undefined) ? "" : item) + ); + } + encodedValue = encodedItemArray.join(","); + } + else { + /* value is a string; simply encode */ + encodedValue = encodeURIComponent(value); + } + paramsArray.push(encodeURIComponent(key) + "=" + encodedValue); + } + } + + return paramsArray.join("&"); +}; + +/** + * Function: urlAppend + * Appends a parameter string to a url. This function includes the logic for + * using the appropriate character (none, & or ?) to append to the url before + * appending the param string. + * + * Parameters: + * url - {String} The url to append to + * paramStr - {String} The param string to append + * + * Returns: + * {String} The new url + */ +OpenLayers.Util.urlAppend = function(url, paramStr) { + var newUrl = url; + if(paramStr) { + var parts = (url + " ").split(/[?&]/); + newUrl += (parts.pop() === " " ? + paramStr : + parts.length ? "&" + paramStr : "?" + paramStr); + } + return newUrl; +}; + +/** + * 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, len=arguments.length; i<len; 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, len=nodes.length; i<len; 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); +}; + +/** + * Property: precision + * {Number} The number of significant digits to retain to avoid + * floating point precision errors. + * + * We use 14 as a "safe" default because, although IEEE 754 double floats + * (standard on most modern operating systems) support up to about 16 + * significant digits, 14 significant digits are sufficient to represent + * sub-millimeter accuracy in any coordinate system that anyone is likely to + * use with OpenLayers. + * + * If DEFAULT_PRECISION is set to 0, the original non-truncating behavior + * of OpenLayers <2.8 is preserved. Be aware that this will cause problems + * with certain projections, e.g. spherical Mercator. + * + */ +OpenLayers.Util.DEFAULT_PRECISION = 14; + +/** + * Function: toFloat + * Convenience method to cast an object to a Number, rounded to the + * desired floating point precision. + * + * Parameters: + * number - {Number} The number to cast and round. + * precision - {Number} An integer suitable for use with + * Number.toPrecision(). Defaults to OpenLayers.Util.DEFAULT_PRECISION. + * If set to 0, no rounding is performed. + * + * Returns: + * {Number} The cast, rounded number. + */ +OpenLayers.Util.toFloat = function (number, precision) { + if (precision == null) { + precision = OpenLayers.Util.DEFAULT_PRECISION; + } + var number; + if (precision == 0) { + number = parseFloat(number); + } else { + number = parseFloat(parseFloat(number).toPrecision(precision)); + } + return number; +}; + +/** + * Function: rad + * + * Parameters: + * x - {Float} + * + * Returns: + * {Float} + */ +OpenLayers.Util.rad = function(x) {return x*Math.PI/180;}; + +/** + * Function: deg + * + * Parameters: + * x - {Float} + * + * Returns: + * {Float} + */ +OpenLayers.Util.deg = function(x) {return x*180/Math.PI;}; + +/** + * Property: VincentyConstants + * {Object} Constants for Vincenty functions. + */ +OpenLayers.Util.VincentyConstants = { + a: 6378137, + b: 6356752.3142, + f: 1/298.257223563 +}; + +/** + * APIFunction: distVincenty + * Given two objects representing points with geographic coordinates, this + * calculates the distance between those points on the surface of an + * ellipsoid. + * + * Parameters: + * p1 - {<OpenLayers.LonLat>} (or any object with both .lat, .lon properties) + * p2 - {<OpenLayers.LonLat>} (or any object with both .lat, .lon properties) + * + * Returns: + * {Float} The distance (in km) between the two input points as measured on an + * ellipsoid. Note that the input point objects must be in geographic + * coordinates (decimal degrees) and the return distance is in kilometers. + */ +OpenLayers.Util.distVincenty = function(p1, p2) { + var ct = OpenLayers.Util.VincentyConstants; + var a = ct.a, b = ct.b, f = ct.f; + + 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 sin