/src/geo/LatLng.js
JavaScript | 60 lines | 44 code | 11 blank | 5 comment | 5 complexity | d018d1fbdb0a23e54f51d298beb7ede6 MD5 | raw file
Possible License(s): BSD-3-Clause
- /*
- CM.LatLng represents a geographical point with latitude and longtitude coordinates.
- */
-
- L.LatLng = function (/*Number*/ rawLat, /*Number*/ rawLng, /*Boolean*/ noWrap) {
- var lat = parseFloat(rawLat),
- lng = parseFloat(rawLng);
-
- if (isNaN(lat) || isNaN(lng)) {
- throw new Error('Invalid LatLng object: (' + rawLat + ', ' + rawLng + ')');
- }
-
- if (noWrap !== true) {
- lat = Math.max(Math.min(lat, 90), -90); // clamp latitude into -90..90
- lng = (lng + 180) % 360 + ((lng < -180 || lng === 180) ? 180 : -180); // wrap longtitude into -180..180
- }
-
- //TODO change to lat() & lng()
- this.lat = lat;
- this.lng = lng;
- };
-
- L.Util.extend(L.LatLng, {
- DEG_TO_RAD: Math.PI / 180,
- RAD_TO_DEG: 180 / Math.PI,
- MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
- });
-
- L.LatLng.prototype = {
- equals: function (/*LatLng*/ obj) {
- if (!(obj instanceof L.LatLng)) {
- return false;
- }
-
- var margin = Math.max(Math.abs(this.lat - obj.lat), Math.abs(this.lng - obj.lng));
- return margin <= L.LatLng.MAX_MARGIN;
- },
-
- toString: function () {
- return 'LatLng(' +
- L.Util.formatNum(this.lat) + ', ' +
- L.Util.formatNum(this.lng) + ')';
- },
-
- // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
- distanceTo: function (/*LatLng*/ other)/*->Double*/ {
- var R = 6378137, // earth radius in meters
- d2r = L.LatLng.DEG_TO_RAD,
- dLat = (other.lat - this.lat) * d2r,
- dLon = (other.lng - this.lng) * d2r,
- lat1 = this.lat * d2r,
- lat2 = other.lat * d2r,
- sin1 = Math.sin(dLat / 2),
- sin2 = Math.sin(dLon / 2);
-
- var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
-
- return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
- }
- };