diff --git a/doc_config/Menu.txt b/doc_config/Menu.txt index 7d685cb73c..1a941749ae 100644 --- a/doc_config/Menu.txt +++ b/doc_config/Menu.txt @@ -458,6 +458,7 @@ Group: OpenLayers { File: Tween (no auto-title, OpenLayers/Tween.js) File: Util (no auto-title, OpenLayers/Util.js) + File: Spherical (no auto-title, OpenLayers/Spherical.js) File: Deprecated (no auto-title, deprecated.js) } # Group: OpenLayers diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 3ffa1b6a84..78b35da424 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -348,7 +348,8 @@ "OpenLayers/Symbolizer/Text.js", "OpenLayers/Symbolizer/Raster.js", "OpenLayers/Lang.js", - "OpenLayers/Lang/en.js" + "OpenLayers/Lang/en.js", + "OpenLayers/Spherical.js" ]; // etc. } diff --git a/lib/OpenLayers/Spherical.js b/lib/OpenLayers/Spherical.js new file mode 100644 index 0000000000..4a8956a5b7 --- /dev/null +++ b/lib/OpenLayers/Spherical.js @@ -0,0 +1,63 @@ +/* Copyright (c) 2006-2012 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. */ + +/** + * Namespace: Spherical + * The OpenLayers.Spherical namespace includes utility functions for + * calculations on the basis of a spherical earth (ignoring ellipsoidal + * effects), which is accurate enough for most purposes. + * + * Relevant links: + * * http://www.movable-type.co.uk/scripts/latlong.html + * * http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical + */ + +OpenLayers.Spherical = OpenLayers.Spherical || {}; + +OpenLayers.Spherical.DEFAULT_RADIUS = 6378137; + +/** + * APIFunction: computeDistanceBetween + * Computes the distance between two LonLats. + * + * Parameters: + * from - {} or {Object} Starting point. A LonLat or + * a JavaScript literal with lon lat properties. + * to - {} or {Object} Ending point. A LonLat or a + * JavaScript literal with lon lat properties. + * radius - {Float} The radius. Optional. Defaults to 6378137 meters. + * + * Returns: + * {Float} The distance in meters. + */ +OpenLayers.Spherical.computeDistanceBetween = function(from, to, radius) { + var R = radius || OpenLayers.Spherical.DEFAULT_RADIUS; + var sinHalfDeltaLon = Math.sin(Math.PI * (to.lon - from.lon) / 360); + var sinHalfDeltaLat = Math.sin(Math.PI * (to.lat - from.lat) / 360); + var a = sinHalfDeltaLat * sinHalfDeltaLat + + sinHalfDeltaLon * sinHalfDeltaLon * Math.cos(Math.PI * from.lat / 180) * Math.cos(Math.PI * to.lat / 180); + return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); +}; + + +/** + * APIFunction: computeHeading + * Computes the heading from one LonLat to another LonLat. + * + * Parameters: + * from - {} or {Object} Starting point. A LonLat or + * a JavaScript literal with lon lat properties. + * to - {} or {Object} Ending point. A LonLat or a + * JavaScript literal with lon lat properties. + * + * Returns: + * {Float} The heading in degrees. + */ +OpenLayers.Spherical.computeHeading = function(from, to) { + var y = Math.sin(Math.PI * (from.lon - to.lon) / 180) * Math.cos(Math.PI * to.lat / 180); + var x = Math.cos(Math.PI * from.lat / 180) * Math.sin(Math.PI * to.lat / 180) - + Math.sin(Math.PI * from.lat / 180) * Math.cos(Math.PI * to.lat / 180) * Math.cos(Math.PI * (from.lon - to.lon) / 180); + return 180 * Math.atan2(y, x) / Math.PI; +};