Permalink
Browse files

Added geolib.getSpeed()

  • Loading branch information...
1 parent cdd4e74 commit a97ac7a8f081bf991b102a4ec5d5aad2cc79b4d1 @manuelbieh committed Feb 7, 2013
Showing with 136 additions and 76 deletions.
  1. +54 −20 README.md
  2. +2 −2 component.json
  3. +35 −25 geolib.js
  4. +3 −3 geolib.min.js
  5. +2 −2 package.json
  6. +33 −24 src/geolib.js
  7. +7 −0 tests/geolib.test.js
View
@@ -5,17 +5,17 @@ A small library to provide some basic geo functions like distance calculation, c
[View demo](http://www.manuel-bieh.de/publikationen/scripts/geolib/demo.html)
-## Methods
+<h2>Methods</h2>
-### geolib.getDistance(object start, object end, [int accuracy])
+<h3>geolib.getDistance(object start, object end[, int accuracy])</h3>
Calculates the distance between two geo coordinates
Takes 2 or 3. First 2 arguments must be an object with a latitude and a longitude property (e.g. {latitude: 52.518611, longitude: 13.408056}). Coordinates can be in sexagesimal or decimal format. 3rd argument is accuracy (in meters). So a calculated distance of 1248 meters with an accuracy of 100 is returned as 1200.
Return value is always an integer and represents the distance in meters.
-#### Examples
+<h4>Examples</h4>
<pre>geolib.getDistance(
{latitude: 51.5103, longitude: 7.49347},
@@ -43,15 +43,15 @@ navigator.geolocation.getCurrentPosition(
);
</pre>
-### geolib.getCenter(array coords)
+<h3>geolib.getCenter(array coords)</h3>
Calculates the geographical center of all points in a collection of geo coordinates
Takes an object or array of coordinates and calculates the center of it.
Returns an object: `{"latitude": centerLat, "longitude": centerLng, "distance": diagonalDistance}`
-#### Examples
+<h4>Examples</h4>
<pre>var spots = {
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
@@ -71,14 +71,14 @@ geolib.getCenter([
]);
</pre>
-### geolib.isPointInside(object latlng, array coords)
+<h3>geolib.isPointInside(object latlng, array coords)</h3>
Checks whether a point is inside of a polygon or not.
Note: the polygon coords must be in correct order!
Returns true or false
-#### Example
+<h4>Example</h4>
<pre>
geolib.isPointInside(
@@ -130,11 +130,11 @@ geolib.orderByDistance({latitude: 51.515, longitude: 7.453619}, {
});
</pre>
-### geolib.findNearest(object latlng, mixed coords, int offset)
+<h3>geolib.findNearest(object latlng, mixed coords[, int offset])</h3>
Finds the nearest coordinate to a reference coordinate.
-#### Examples
+<h4>Examples</h4>
<pre>var spots = {
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
@@ -150,13 +150,29 @@ Finds the nearest coordinate to a reference coordinate.
geolib.findNearest(spots['Dortmund U-Tower'], spots, 1)
</pre>
-### geolib.getPathLength(mixed coords)
+<h3>geolib.getPathLength(mixed coords)</h3>
+
+Calculates the length of a collection of coordinates
+
+Returns the length of the path in kilometers
+
+<h4>Example</h4>
+
+<pre>
+// Calculate distance from Berlin via Dortmund to London
+geolib.getPathLength([
+ {latitude: 52.516272, longitude: 13.377722}, // Berlin
+ {latitude: 51.515, longitude: 7.453619}, // Dortmund
+ {latitude: 51.503333, longitude: -0.119722} // London
+]); // -> 945235</pre>
+
+<h3>geolib.getPathLength(mixed coords)</h3>
Calculates the length of a collection of coordinates
Returns the length of the path in kilometers
-#### Example
+<h4>Example</h4>
<pre>
// Calculate distance from Berlin via Dortmund to London
@@ -166,11 +182,29 @@ geolib.getPathLength([
{latitude: 51.503333, longitude: -0.119722} // London
]); // -> 945235</pre>
-### geolib.convertUnit(string unit, float distance, [int round])
+
+<h3>geolib.getSpeed(coords, coords[, options])</h3>
+
+Calculates the speed between to points within a given time span.
+
+Returns the speed in <em>options.unit</em> (default is km/h).
+
+<h4>Example</h4>
+
+<pre>
+geolib.getSpeed(
+ {lat: 51.567294, lng: 7.38896, time: 1360231200880},
+ {lat: 52.54944, lng: 13.468509, time: 1360245600880},
+ {unit: 'mph'}
+); // -> 66.9408 (mph)</pre>
+
+
+
+<h3>geolib.convertUnit(string unit, float distance[, int round])</h3>
Converts a given distance (in meters) to another unit.
-#### Parameters
+<h4>Parameters</h4>
`unit` can be one of:
@@ -188,32 +222,32 @@ Converts a given distance (in meters) to another unit.
`round` fractional digits
-#### Example
+<h4>Example</h4>
`geolib.convertUnit('km', 14213, 2) // -> 14,21`
-### geolib.sexagesimal2decimal(string coord)
+<h3>geolib.sexagesimal2decimal(string coord)</h3>
Converts a sexagesimal coordinate to decimal format
-#### Example
+<h4>Example</h4>
`geolib.sexagesimal2decimal("51° 29' 46\" N")`
-### geolib.decimal2sexagesimal(float coord)
+<h3>geolib.decimal2sexagesimal(float coord)</h3>
Converts a decimal coordinate to sexagesimal format
-#### Example
+<h4>Example</h4>
`geolib.decimal2sexagesimal(51.49611111); // -> 51° 29' 46.00`
-### geolib.useDecimal(mixed coordinate)
+<h3>geolib.useDecimal(mixed coordinate)</h3>
Checks if a coordinate is already in decimal format and, if not, converts it to
-#### Example
+<h4>Example</h4>
<pre>geolib.useDecimal("51° 29' 46\" N"); // -> 51.59611111
geolib.useDecimal(51.59611111) // -> 51.59611111</pre>
View
@@ -13,7 +13,7 @@
"url": "http://github.com/manuelbieh/geolib.git"
},
"devDependencies": {
- "grunt": "~0.3.9",
+ "grunt": "~0.3.17",
"grunt-contrib-clean": "~0.3.0",
"grunt-contrib-copy": "~0.3.0"
},
@@ -35,6 +35,6 @@
"scripts": {
"test": "grunt travis --verbose"
},
- "version": "1.2.6",
+ "version": "1.2.7",
"main": "./geolib"
}
View
@@ -1,15 +1,15 @@
-/*! geolib 1.2.6 by Manuel Bieh
+/*! geolib 1.2.7 by Manuel Bieh
* A small library to provide some basic geo functions like distance calculation,
* conversion of decimal coordinates to sexagesimal and vice versa, etc.
* WGS 84 (World Geodetic System 1984)
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
-* @version 1.2.6
+* @version 1.2.7
* @license LGPL
**/
-/*global geolib:true require:true module:true window:true*/
+/*global console:true geolib:true require:true module:true window:true*/
(function (window, undefined) {
var radius = 6378137; // Earth radius
@@ -23,6 +23,18 @@
distance: 0,
+ measures: {
+ m: 1,
+ km: 0.001,
+ cm: 100,
+ mm: 1000,
+ mi: (1 / 1609.344),
+ sm: (1 / 1852.216),
+ ft: (100 / 30.48),
+ "in": (100 / 2.54),
+ yd: (1 / 0.9144)
+ },
+
/**
* Get the key names for a geo point.
*
@@ -631,6 +643,22 @@
},
+ getSpeed: function(start, end, options) {
+
+ var unit = options && options.unit || 'km';
+
+ if(unit == 'mph') {
+ unit = 'mi';
+ }
+
+ var distance = geolib.getDistance(start, end);
+ var time = ((end.time*1)/1000) - ((start.time*1)/1000);
+ var mPerHr = (distance/time)*3600;
+ var speed = Math.round(mPerHr * geolib.measures[unit] * 10000)/10000;
+ return speed;
+
+ },
+
/**
* Converts a distance from meters to km, mm, cm, mi, ft, in or yd
*
@@ -655,30 +683,12 @@
unit = unit || 'm';
round = (null == round ? 4 : round);
- switch(unit) {
-
- case 'm': // Meter
- return geolib.round(distance, round);
- case 'km': // Kilometer
- return geolib.round(distance / 1000, round);
- case 'cm': // Centimeter
- return geolib.round(distance * 100, round);
- case 'mm': // Millimeter
- return geolib.round(distance * 1000, round);
- case 'mi': // Miles
- return geolib.round(distance * (1 / 1609.344), round);
- case 'sm': // Seamiles
- return geolib.round(distance * (1 / 1852.216), round);
- case 'ft': // Feet
- return geolib.round(distance * (100 / 30.48), round);
- case 'in': // Inch
- return geolib.round(distance * 100 / 2.54, round);
- case 'yd': // Yards
- return geolib.round(distance * (1 / 0.9144), round);
+ if(typeof geolib.measures[unit] !== 'undefined') {
+ return geolib.round(distance * geolib.measures[unit], round);
+ } else {
+ throw new Error('Unknown unit for conversion.');
}
- return distance;
-
},
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -13,7 +13,7 @@
"url": "http://github.com/manuelbieh/geolib.git"
},
"devDependencies": {
- "grunt": "~0.3.9",
+ "grunt": "~0.3.17",
"grunt-contrib-clean": "~0.3.0",
"grunt-contrib-copy": "~0.3.0"
},
@@ -35,6 +35,6 @@
"scripts": {
"test": "grunt travis --verbose"
},
- "version": "1.2.6",
+ "version": "1.2.7",
"main": "./geolib"
}
View
@@ -5,11 +5,10 @@
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
-* @version 1.2.1
* @license LGPL
**/
-/*global geolib:true require:true module:true window:true*/
+/*global console:true geolib:true require:true module:true window:true*/
(function (window, undefined) {
var radius = 6378137; // Earth radius
@@ -23,6 +22,18 @@
distance: 0,
+ measures: {
+ m: 1,
+ km: 0.001,
+ cm: 100,
+ mm: 1000,
+ mi: (1 / 1609.344),
+ sm: (1 / 1852.216),
+ ft: (100 / 30.48),
+ "in": (100 / 2.54),
+ yd: (1 / 0.9144)
+ },
+
/**
* Get the key names for a geo point.
*
@@ -631,6 +642,22 @@
},
+ getSpeed: function(start, end, options) {
+
+ var unit = options && options.unit || 'km';
+
+ if(unit == 'mph') {
+ unit = 'mi';
+ }
+
+ var distance = geolib.getDistance(start, end);
+ var time = ((end.time*1)/1000) - ((start.time*1)/1000);
+ var mPerHr = (distance/time)*3600;
+ var speed = Math.round(mPerHr * geolib.measures[unit] * 10000)/10000;
+ return speed;
+
+ },
+
/**
* Converts a distance from meters to km, mm, cm, mi, ft, in or yd
*
@@ -655,30 +682,12 @@
unit = unit || 'm';
round = (null == round ? 4 : round);
- switch(unit) {
-
- case 'm': // Meter
- return geolib.round(distance, round);
- case 'km': // Kilometer
- return geolib.round(distance / 1000, round);
- case 'cm': // Centimeter
- return geolib.round(distance * 100, round);
- case 'mm': // Millimeter
- return geolib.round(distance * 1000, round);
- case 'mi': // Miles
- return geolib.round(distance * (1 / 1609.344), round);
- case 'sm': // Seamiles
- return geolib.round(distance * (1 / 1852.216), round);
- case 'ft': // Feet
- return geolib.round(distance * (100 / 30.48), round);
- case 'in': // Inch
- return geolib.round(distance * 100 / 2.54, round);
- case 'yd': // Yards
- return geolib.round(distance * (1 / 0.9144), round);
+ if(typeof geolib.measures[unit] !== 'undefined') {
+ return geolib.round(distance * geolib.measures[unit], round);
+ } else {
+ throw new Error('Unknown unit for conversion.');
}
- return distance;
-
},
Oops, something went wrong.

0 comments on commit a97ac7a

Please sign in to comment.