From fb4304efc461bd95bbe4e882bd1a238584221b04 Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Tue, 22 Nov 2016 23:57:45 -0300 Subject: [PATCH] Added destinationPoint function --- README.md | 5 +++-- geopoint.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c06a345..4a11d40 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ GeoPoint represents a geographic point for node.js and the browser, and provides ## Installation - npm install geopoint + npm install geopoint ## Usage @@ -25,6 +25,7 @@ var GeoPoint = require('geopoint'), * `.longitude(inRadians)`: Return the point's longitude. By default, the longitude is in degrees, unless `inRadians` is `true` * `.distanceTo(point, inKilometers)`: Calculate the distance to another `GeoPoint` instance. By default, the distance is calculated in miles, unless `inKilometers` is `true` * `.boundingCoordinates(distance, radius, inKilometers)`: Calculates the bounding coordinates of `distance` from the point and returns an array with the SW and NE points of the bounding box . If `radius` is not provided, the radius of the Earth will be used. The distance is calculated in miles unless `inKilometers` is `true` +* `.destinationPoint(distance, radius, inKilometers)`: Given a bearing, and distance, calculate the destina­tion `GeoPoint`. The distance is calculated in miles unless `inKilometers` is `true` ## Static Methods @@ -40,7 +41,7 @@ The `GeoPoint` constructor is exposed via `window.GeoPoint`. ## Running Tests Cachetree tests require [Mocha](http://visionmedia.github.com/mocha/) and can be run with either `npm test`. You can specify Mocha options, such as the reporter, by adding a [mocha.opts](http://visionmedia.github.com/mocha/#mocha.opts) file, which is ignored by git, to the `test` directory. - + # Credits This library is derived from the code presented in [Finding Points Within a Distance of a Latitude/Longitude Using Bounding Coordinates](http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates) by [Jan Philip Matuschek](http://janmatuschek.de/Contact). diff --git a/geopoint.js b/geopoint.js index 0c2cadd..f769040 100644 --- a/geopoint.js +++ b/geopoint.js @@ -3,7 +3,7 @@ * * This library is derived from the Java code originally published at * http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates - * + * * @author Jan Philip Matuschek * @version 22 September 2010 */ @@ -158,6 +158,34 @@ return [new GeoPoint(minLat, minLon, true), new GeoPoint(maxLat, maxLon, true)]; }; + /** + * Get destination point from distance and bearing + * + * @param {Number} distance distance from the point + * @param {Number} bearing degrees from 0 to 360 (clockwise from north) + * @param {Boolean} inKilometers true to return the distance in kilometers + * @return {GeoPoint} destina­tion point + */ + GeoPoint.prototype.destinationPoint = function(distance, bearing, inKilometers) { + if (!isNumber(distance) || distance <= 0) { + throw new Error('Invalid distance'); + } + if (bearing === true || bearing === false) { + inKilometers = bearing; + bearing = 0; + } + if(!isNumber(bearing)) bearing = 0; + bearing = bearing % 360; + + var radius = inKilometers === true ? EARTH_RADIUS_KM : EARTH_RADIUS_MI; + lat = this.latitude(true), + lon = this.longitude(true), + radDist = distance / radius, + lat2 = Math.asin( Math.sin(lat)*Math.cos(radDist) + Math.cos(lat)*Math.sin(radDist)*Math.cos(bearing) ), + lon2 = lon + Math.atan2(Math.sin(bearing)*Math.sin(radDist)*Math.cos(lat), Math.cos(radDist)-Math.sin(lat)*Math.sin(lat2)); + return new GeoPoint(lat2, lon2, true); + }; + /** * Convert degrees to radians *