Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added destinationPoint function #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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).
30 changes: 29 additions & 1 deletion geopoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down