ParseGeoPoint.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. /**
  12. * Creates a new GeoPoint with any of the following forms:<br>
  13. * <pre>
  14. * new GeoPoint(otherGeoPoint)
  15. * new GeoPoint(30, 30)
  16. * new GeoPoint([30, 30])
  17. * new GeoPoint({latitude: 30, longitude: 30})
  18. * new GeoPoint() // defaults to (0, 0)
  19. * </pre>
  20. * <p>Represents a latitude / longitude point that may be associated
  21. * with a key in a ParseObject or used as a reference point for geo queries.
  22. * This allows proximity-based queries on the key.</p>
  23. *
  24. * <p>Only one key in a class may contain a GeoPoint.</p>
  25. *
  26. * <p>Example:<pre>
  27. * var point = new Parse.GeoPoint(30.0, -20.0);
  28. * var object = new Parse.Object("PlaceObject");
  29. * object.set("location", point);
  30. * object.save();</pre></p>
  31. * @alias Parse.GeoPoint
  32. */
  33. /* global navigator */
  34. class ParseGeoPoint {
  35. /*:: _latitude: number;*/
  36. /*:: _longitude: number;*/
  37. /**
  38. * @param {(Number[]|Object|Number)} options Either a list of coordinate pairs, an object with `latitude`, `longitude`, or the latitude or the point.
  39. * @param {Number} longitude The longitude of the GeoPoint
  40. */
  41. constructor(arg1
  42. /*: Array<number> |
  43. { latitude: number; longitude: number } |
  44. number*/
  45. , arg2
  46. /*:: ?: number*/
  47. ) {
  48. if (Array.isArray(arg1)) {
  49. ParseGeoPoint._validate(arg1[0], arg1[1]);
  50. this._latitude = arg1[0];
  51. this._longitude = arg1[1];
  52. } else if (typeof arg1 === 'object') {
  53. ParseGeoPoint._validate(arg1.latitude, arg1.longitude);
  54. this._latitude = arg1.latitude;
  55. this._longitude = arg1.longitude;
  56. } else if (arg1 !== undefined && arg2 !== undefined) {
  57. ParseGeoPoint._validate(arg1, arg2);
  58. this._latitude = arg1;
  59. this._longitude = arg2;
  60. } else {
  61. this._latitude = 0;
  62. this._longitude = 0;
  63. }
  64. }
  65. /**
  66. * North-south portion of the coordinate, in range [-90, 90].
  67. * Throws an exception if set out of range in a modern browser.
  68. * @property latitude
  69. * @type Number
  70. */
  71. get latitude()
  72. /*: number*/
  73. {
  74. return this._latitude;
  75. }
  76. set latitude(val
  77. /*: number*/
  78. ) {
  79. ParseGeoPoint._validate(val, this.longitude);
  80. this._latitude = val;
  81. }
  82. /**
  83. * East-west portion of the coordinate, in range [-180, 180].
  84. * Throws if set out of range in a modern browser.
  85. * @property longitude
  86. * @type Number
  87. */
  88. get longitude()
  89. /*: number*/
  90. {
  91. return this._longitude;
  92. }
  93. set longitude(val
  94. /*: number*/
  95. ) {
  96. ParseGeoPoint._validate(this.latitude, val);
  97. this._longitude = val;
  98. }
  99. /**
  100. * Returns a JSON representation of the GeoPoint, suitable for Parse.
  101. * @return {Object}
  102. */
  103. toJSON()
  104. /*: { __type: string; latitude: number; longitude: number }*/
  105. {
  106. ParseGeoPoint._validate(this._latitude, this._longitude);
  107. return {
  108. __type: 'GeoPoint',
  109. latitude: this._latitude,
  110. longitude: this._longitude
  111. };
  112. }
  113. equals(other
  114. /*: mixed*/
  115. )
  116. /*: boolean*/
  117. {
  118. return other instanceof ParseGeoPoint && this.latitude === other.latitude && this.longitude === other.longitude;
  119. }
  120. /**
  121. * Returns the distance from this GeoPoint to another in radians.
  122. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  123. * @return {Number}
  124. */
  125. radiansTo(point
  126. /*: ParseGeoPoint*/
  127. )
  128. /*: number*/
  129. {
  130. const d2r = Math.PI / 180.0;
  131. const lat1rad = this.latitude * d2r;
  132. const long1rad = this.longitude * d2r;
  133. const lat2rad = point.latitude * d2r;
  134. const long2rad = point.longitude * d2r;
  135. const sinDeltaLatDiv2 = Math.sin((lat1rad - lat2rad) / 2);
  136. const sinDeltaLongDiv2 = Math.sin((long1rad - long2rad) / 2); // Square of half the straight line chord distance between both points.
  137. let a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2;
  138. a = Math.min(1.0, a);
  139. return 2 * Math.asin(Math.sqrt(a));
  140. }
  141. /**
  142. * Returns the distance from this GeoPoint to another in kilometers.
  143. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  144. * @return {Number}
  145. */
  146. kilometersTo(point
  147. /*: ParseGeoPoint*/
  148. )
  149. /*: number*/
  150. {
  151. return this.radiansTo(point) * 6371.0;
  152. }
  153. /**
  154. * Returns the distance from this GeoPoint to another in miles.
  155. * @param {Parse.GeoPoint} point the other Parse.GeoPoint.
  156. * @return {Number}
  157. */
  158. milesTo(point
  159. /*: ParseGeoPoint*/
  160. )
  161. /*: number*/
  162. {
  163. return this.radiansTo(point) * 3958.8;
  164. }
  165. /*
  166. * Throws an exception if the given lat-long is out of bounds.
  167. */
  168. static _validate(latitude
  169. /*: number*/
  170. , longitude
  171. /*: number*/
  172. ) {
  173. if (isNaN(latitude) || isNaN(longitude) || typeof latitude !== 'number' || typeof longitude !== 'number') {
  174. throw new TypeError('GeoPoint latitude and longitude must be valid numbers');
  175. }
  176. if (latitude < -90.0) {
  177. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' < -90.0.');
  178. }
  179. if (latitude > 90.0) {
  180. throw new TypeError('GeoPoint latitude out of bounds: ' + latitude + ' > 90.0.');
  181. }
  182. if (longitude < -180.0) {
  183. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' < -180.0.');
  184. }
  185. if (longitude > 180.0) {
  186. throw new TypeError('GeoPoint longitude out of bounds: ' + longitude + ' > 180.0.');
  187. }
  188. }
  189. /**
  190. * Creates a GeoPoint with the user's current location, if available.
  191. * Calls options.success with a new GeoPoint instance or calls options.error.
  192. * @static
  193. */
  194. static current() {
  195. return navigator.geolocation.getCurrentPosition(location => {
  196. return new ParseGeoPoint(location.coords.latitude, location.coords.longitude);
  197. });
  198. }
  199. }
  200. export default ParseGeoPoint;