Permalink
Browse files

Fixes #13

  • Loading branch information...
1 parent f84a90a commit 48cf8490646e17bec16b87dcf68d96b35d268eb3 @manuelbieh committed Oct 26, 2012
Showing with 265 additions and 259 deletions.
  1. +124 −121 geolib.js
  2. +2 −2 geolib.min.js
  3. +1 −1 package.json
  4. +136 −130 src/geolib.elevation.js
  5. +2 −5 src/geolib.js
View
245 geolib.js
@@ -5,7 +5,7 @@
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
-* @version 1.2.1
+* @version 1.2.3
* @license LGPL
**/
@@ -798,147 +798,150 @@
}
// we're in a browser
- if(typeof window.navigator != 'undefined') {
- window.geolib = geolib;
- // we're working with node.js, hipster!
- // (experimental support)
- } else if (typeof module != 'undefined') {
+ window.geolib = geolib;
+ if (typeof module != 'undefined') {
module.exports = geolib;
}
}(this));
-/* Optional elevation addon requires Googlemaps API JS */
+(function(global) {
-/*global google:true geolib:true require:true module:true elevationResult*/
-/**
-* @param Array Collection of coords [{latitude: 51.510, longitude: 7.1321}, {latitude: 49.1238, longitude: "8° 30' W"}, ...]
-*
-* @return Array [{lat:#lat, lng:#lng, elev:#elev},....]}
-*/
-geolib.getElevation = function() {
- if (typeof window.navigator !== 'undefined') {
- geolib.getElevationClient.apply(this, arguments);
- } else {
- geolib.getElevationServer.apply(this, arguments);
- }
-};
+ var geolib = global.geolib;
-geolib.getElevationClient = function(coords, cb) {
+ /* Optional elevation addon requires Googlemaps API JS */
- if (!window.google) {
- throw new Error("Google maps api not loaded");
- }
-
- if (coords.length === 0) {
- return cb(null, null);
- }
+ /*global google:true geolib:true require:true module:true elevationResult*/
+ /**
+ * @param Array Collection of coords [{latitude: 51.510, longitude: 7.1321}, {latitude: 49.1238, longitude: "8° 30' W"}, ...]
+ *
+ * @return Array [{lat:#lat, lng:#lng, elev:#elev},....]}
+ */
+ geolib.getElevation = function() {
+ if (typeof window.navigator !== 'undefined') {
+ geolib.getElevationClient.apply(this, arguments);
+ } else {
+ geolib.getElevationServer.apply(this, arguments);
+ }
+ };
- if (coords.length === 1) {
- return cb(new Error("getElevation requires at least 2 points."));
- }
+ geolib.getElevationClient = function(coords, cb) {
- var path = [];
- var keys = geolib.getKeys(coords[0]);
- var latitude = keys.latitude;
- var longitude = keys.longitude;
+ if (!window.google) {
+ throw new Error("Google maps api not loaded");
+ }
- for(var i = 0; i < coords.length; i++) {
- path.push(new google.maps.LatLng(
- geolib.useDecimal(coords[i][latitude]),
- geolib.useDecimal(coords[i][longitude])
- ));
- }
+ if (coords.length === 0) {
+ return cb(null, null);
+ }
- var positionalRequest = {
- 'path': path,
- 'samples': path.length
- };
- var elevationService = new google.maps.ElevationService();
- elevationService.getElevationAlongPath(positionalRequest,function (results, status) {
- geolib.elevationHandler(results, status, coords, keys, cb);
- });
+ if (coords.length === 1) {
+ return cb(new Error("getElevation requires at least 2 points."));
+ }
-};
+ var path = [];
+ var keys = geolib.getKeys(coords[0]);
+ var latitude = keys.latitude;
+ var longitude = keys.longitude;
-geolib.getElevationServer = function(coords, cb) {
+ for(var i = 0; i < coords.length; i++) {
+ path.push(new google.maps.LatLng(
+ geolib.useDecimal(coords[i][latitude]),
+ geolib.useDecimal(coords[i][longitude])
+ ));
+ }
- if (coords.length === 0) {
- return cb(null, null);
- }
+ var positionalRequest = {
+ 'path': path,
+ 'samples': path.length
+ };
+ var elevationService = new google.maps.ElevationService();
+ elevationService.getElevationAlongPath(positionalRequest,function (results, status) {
+ geolib.elevationHandler(results, status, coords, keys, cb);
+ });
- if (coords.length === 1) {
- return cb(new Error("getElevation requires at least 2 points."));
- }
+ };
- var gm = require('googlemaps');
- var path = [];
- var keys = geolib.getKeys(coords[0]);
- //coords[0]
- var latitude = keys.latitude;
- var longitude = keys.longitude;
+ geolib.getElevationServer = function(coords, cb) {
- for(var i = 0; i < coords.length; i++) {
- path.push(geolib.useDecimal(coords[i][latitude]) + ',' +
- geolib.useDecimal(coords[i][longitude]));
- }
+ if (coords.length === 0) {
+ return cb(null, null);
+ }
- gm.elevationFromPath(path.join('|'), path.length, function(err, results) {
- geolib.elevationHandler(results.results, results.status, coords, keys, cb);
- });
-
-},
-
-geolib.elevationHandler = function(results, status, coords, keys, cb){
- var latsLngsElevs = [];
- var latitude = keys.latitude;
- var longitude = keys.longitude;
- if (status == "OK" ) {
- for (var i = 0; i < results.length; i++) {
- latsLngsElevs.push({
- "lat":coords[i][latitude],
- "lng":coords[i][longitude],
- "elev":results[i].elevation
- });
+ if (coords.length === 1) {
+ return cb(new Error("getElevation requires at least 2 points."));
}
- cb(null, latsLngsElevs);
- } else {
- cb(new Error("Could not get elevation using Google's API"), elevationResult.status);
- }
-};
-/**
-* @param Array [{lat:#lat, lng:#lng, elev:#elev},....]}
-*
-* @return Number % grade
-*/
-geolib.getGrade = function(coords){
- var keys = geolib.getKeys(coords[0]);
- var elevation = keys.elevation;
- var rise = Math.abs(coords[coords.length-1][elevation] - coords[0][elevation]);
- var run = geolib.getPathLength(coords);
- return Math.floor((rise/run)*100);
-};
+ var gm = require('googlemaps');
+ var path = [];
+ var keys = geolib.getKeys(coords[0]);
+ //coords[0]
+ var latitude = keys.latitude;
+ var longitude = keys.longitude;
-/**
-* @param Array [{lat:#lat, lng:#lng, elev:#elev},....]}
-*
-* @return Object {gain:#gain, loss:#loss}
-*/
-geolib.getTotalElevationGainAndLoss = function(coords){
- var keys = geolib.getKeys(coords[0]);
- var elevation = keys.elevation;
- var gain = 0;
- var loss = 0;
- for(var i = 0; i < coords.length - 1; i++){
- var deltaElev = coords[i][elevation] - coords[i + 1][elevation];
- if (deltaElev > 0) {
- loss += deltaElev;
+ for(var i = 0; i < coords.length; i++) {
+ path.push(geolib.useDecimal(coords[i][latitude]) + ',' +
+ geolib.useDecimal(coords[i][longitude]));
+ }
+
+ gm.elevationFromPath(path.join('|'), path.length, function(err, results) {
+ geolib.elevationHandler(results.results, results.status, coords, keys, cb);
+ });
+
+ },
+
+ geolib.elevationHandler = function(results, status, coords, keys, cb){
+ var latsLngsElevs = [];
+ var latitude = keys.latitude;
+ var longitude = keys.longitude;
+ if (status == "OK" ) {
+ for (var i = 0; i < results.length; i++) {
+ latsLngsElevs.push({
+ "lat":coords[i][latitude],
+ "lng":coords[i][longitude],
+ "elev":results[i].elevation
+ });
+ }
+ cb(null, latsLngsElevs);
} else {
- gain += Math.abs(deltaElev);
+ cb(new Error("Could not get elevation using Google's API"), elevationResult.status);
}
- }
- return {
- "gain": gain,
- "loss": loss
};
-};
+
+ /**
+ * @param Array [{lat:#lat, lng:#lng, elev:#elev},....]}
+ *
+ * @return Number % grade
+ */
+ geolib.getGrade = function(coords){
+ var keys = geolib.getKeys(coords[0]);
+ var elevation = keys.elevation;
+ var rise = Math.abs(coords[coords.length-1][elevation] - coords[0][elevation]);
+ var run = geolib.getPathLength(coords);
+ return Math.floor((rise/run)*100);
+ };
+
+ /**
+ * @param Array [{lat:#lat, lng:#lng, elev:#elev},....]}
+ *
+ * @return Object {gain:#gain, loss:#loss}
+ */
+ geolib.getTotalElevationGainAndLoss = function(coords){
+ var keys = geolib.getKeys(coords[0]);
+ var elevation = keys.elevation;
+ var gain = 0;
+ var loss = 0;
+ for(var i = 0; i < coords.length - 1; i++){
+ var deltaElev = coords[i][elevation] - coords[i + 1][elevation];
+ if (deltaElev > 0) {
+ loss += deltaElev;
+ } else {
+ gain += Math.abs(deltaElev);
+ }
+ }
+ return {
+ "gain": gain,
+ "loss": loss
+ };
+ };
+
+})(this);
Oops, something went wrong.

0 comments on commit 48cf849

Please sign in to comment.