index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. // http://en.wikipedia.org/wiki/Haversine_formula
  4. // http://www.movable-type.co.uk/scripts/latlong.html
  5. var helpers_1 = require("@turf/helpers");
  6. var invariant_1 = require("@turf/invariant");
  7. /**
  8. * Takes a {@link Point} and calculates the location of a destination point given a distance in
  9. * degrees, radians, miles, or kilometers; and bearing in degrees.
  10. * This uses the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula) to account for global curvature.
  11. *
  12. * @name destination
  13. * @param {Coord} origin starting point
  14. * @param {number} distance distance from the origin point
  15. * @param {number} bearing ranging from -180 to 180
  16. * @param {Object} [options={}] Optional parameters
  17. * @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
  18. * @param {Object} [options.properties={}] Translate properties to Point
  19. * @returns {Feature<Point>} destination point
  20. * @example
  21. * var point = turf.point([-75.343, 39.984]);
  22. * var distance = 50;
  23. * var bearing = 90;
  24. * var options = {units: 'miles'};
  25. *
  26. * var destination = turf.destination(point, distance, bearing, options);
  27. *
  28. * //addToMap
  29. * var addToMap = [point, destination]
  30. * destination.properties['marker-color'] = '#f00';
  31. * point.properties['marker-color'] = '#0f0';
  32. */
  33. function destination(origin, distance, bearing, options) {
  34. if (options === void 0) { options = {}; }
  35. // Handle input
  36. var coordinates1 = invariant_1.getCoord(origin);
  37. var longitude1 = helpers_1.degreesToRadians(coordinates1[0]);
  38. var latitude1 = helpers_1.degreesToRadians(coordinates1[1]);
  39. var bearingRad = helpers_1.degreesToRadians(bearing);
  40. var radians = helpers_1.lengthToRadians(distance, options.units);
  41. // Main
  42. var latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians) +
  43. Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearingRad));
  44. var longitude2 = longitude1 +
  45. Math.atan2(Math.sin(bearingRad) * Math.sin(radians) * Math.cos(latitude1), Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2));
  46. var lng = helpers_1.radiansToDegrees(longitude2);
  47. var lat = helpers_1.radiansToDegrees(latitude2);
  48. return helpers_1.point([lng, lat], options.properties);
  49. }
  50. exports.default = destination;