1
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
|
2
|
* full list of contributors). Published under the 2-clause BSD license.
|
3
|
* See license.txt in the OpenLayers distribution or repository for the
|
4
|
* full text of the license. */
|
5
|
|
6
|
/**
|
7
|
* @requires OpenLayers/SingleFile.js
|
8
|
*/
|
9
|
|
10
|
/**
|
11
|
* Namespace: Spherical
|
12
|
* The OpenLayers.Spherical namespace includes utility functions for
|
13
|
* calculations on the basis of a spherical earth (ignoring ellipsoidal
|
14
|
* effects), which is accurate enough for most purposes.
|
15
|
*
|
16
|
* Relevant links:
|
17
|
* * http://www.movable-type.co.uk/scripts/latlong.html
|
18
|
* * http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical
|
19
|
*/
|
20
|
|
21
|
OpenLayers.Spherical = OpenLayers.Spherical || {};
|
22
|
|
23
|
OpenLayers.Spherical.DEFAULT_RADIUS = 6378137;
|
24
|
|
25
|
/**
|
26
|
* APIFunction: computeDistanceBetween
|
27
|
* Computes the distance between two LonLats.
|
28
|
*
|
29
|
* Parameters:
|
30
|
* from - {<OpenLayers.LonLat>} or {Object} Starting point. A LonLat or
|
31
|
* a JavaScript literal with lon lat properties.
|
32
|
* to - {<OpenLayers.LonLat>} or {Object} Ending point. A LonLat or a
|
33
|
* JavaScript literal with lon lat properties.
|
34
|
* radius - {Float} The radius. Optional. Defaults to 6378137 meters.
|
35
|
*
|
36
|
* Returns:
|
37
|
* {Float} The distance in meters.
|
38
|
*/
|
39
|
OpenLayers.Spherical.computeDistanceBetween = function(from, to, radius) {
|
40
|
var R = radius || OpenLayers.Spherical.DEFAULT_RADIUS;
|
41
|
var sinHalfDeltaLon = Math.sin(Math.PI * (to.lon - from.lon) / 360);
|
42
|
var sinHalfDeltaLat = Math.sin(Math.PI * (to.lat - from.lat) / 360);
|
43
|
var a = sinHalfDeltaLat * sinHalfDeltaLat +
|
44
|
sinHalfDeltaLon * sinHalfDeltaLon * Math.cos(Math.PI * from.lat / 180) * Math.cos(Math.PI * to.lat / 180);
|
45
|
return 2 * R * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
46
|
};
|
47
|
|
48
|
|
49
|
/**
|
50
|
* APIFunction: computeHeading
|
51
|
* Computes the heading from one LonLat to another LonLat.
|
52
|
*
|
53
|
* Parameters:
|
54
|
* from - {<OpenLayers.LonLat>} or {Object} Starting point. A LonLat or
|
55
|
* a JavaScript literal with lon lat properties.
|
56
|
* to - {<OpenLayers.LonLat>} or {Object} Ending point. A LonLat or a
|
57
|
* JavaScript literal with lon lat properties.
|
58
|
*
|
59
|
* Returns:
|
60
|
* {Float} The heading in degrees.
|
61
|
*/
|
62
|
OpenLayers.Spherical.computeHeading = function(from, to) {
|
63
|
var y = Math.sin(Math.PI * (from.lon - to.lon) / 180) * Math.cos(Math.PI * to.lat / 180);
|
64
|
var x = Math.cos(Math.PI * from.lat / 180) * Math.sin(Math.PI * to.lat / 180) -
|
65
|
Math.sin(Math.PI * from.lat / 180) * Math.cos(Math.PI * to.lat / 180) * Math.cos(Math.PI * (from.lon - to.lon) / 180);
|
66
|
return 180 * Math.atan2(y, x) / Math.PI;
|
67
|
};
|