Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes #13

  • Loading branch information...
commit 48cf8490646e17bec16b87dcf68d96b35d268eb3 1 parent f84a90a
@manuelbieh authored
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);
View
4 geolib.min.js
@@ -5,7 +5,7 @@
*
* @author Manuel Bieh
* @url http://www.manuelbieh.com/
-* @version 1.2.1
+* @version 1.2.3
* @license LGPL
**/
-(function(e,t){var n=6378137,r=/^([0-9]{1,3})°\s*([0-9]{1,3})'\s*(([0-9]{1,3}(\.([0-9]{1,2}))?)"\s*)?([NEOSW]?)$/,i={decimal:{},sexagesimal:{},distance:0,getKeys:function(e){var t=e.hasOwnProperty("lat")?"lat":"latitude",n=(e.hasOwnProperty("lng")?"lng":!1)||(e.hasOwnProperty("long")?"long":!1)||"longitude",r=(e.hasOwnProperty("alt")?"alt":!1)||(e.hasOwnProperty("altitude")?"altitude":!1)||(e.hasOwnProperty("elev")?"elev":!1)||"elevation";return{latitude:t,longitude:n,elevation:r}},getDistance:function(e,t,n){var r=i.getKeys(e),s=r.latitude,o=r.longitude,u=r.elevation;n=Math.floor(n)||1;var a={},f={};a[s]=i.useDecimal(e[s]),a[o]=i.useDecimal(e[o]),f[s]=i.useDecimal(t[s]),f[o]=i.useDecimal(t[o]);var l=6378137,c=6356752.314245,h=1/298.257223563,p=(f[o]-a[o]).toRad(),d,v,m,g,y,b,w=Math.atan((1-h)*Math.tan(parseFloat(a[s]).toRad())),E=Math.atan((1-h)*Math.tan(parseFloat(f[s]).toRad())),S=Math.sin(w),x=Math.cos(w),T=Math.sin(E),N=Math.cos(E),C=p,k,L=100;do{var A=Math.sin(C),O=Math.cos(C);b=Math.sqrt(N*A*N*A+(x*T-S*N*O)*(x*T-S*N*O));if(b===0)return i.distance=0;d=S*T+x*N*O,v=Math.atan2(b,d),m=x*N*A/b,g=1-m*m,y=d-2*S*T/g,isNaN(y)&&(y=0);var M=h/16*g*(4+h*(4-3*g));k=C,C=p+(1-M)*h*m*(v+M*b*(y+M*d*(-1+2*y*y)))}while(Math.abs(C-k)>1e-12&&--L>0);if(L===0)return NaN;var _=g*(l*l-c*c)/(c*c),D=1+_/16384*(4096+_*(-768+_*(320-175*_))),P=_/1024*(256+_*(-128+_*(74-47*_))),H=P*b*(y+P/4*(d*(-1+2*y*y)-P/6*y*(-3+4*b*b)*(-3+4*y*y))),B=c*D*(v-H);B=B.toFixed(3);if(e.hasOwnProperty(u)&&t.hasOwnProperty(u)){var j=Math.abs(e[u]-t[u]);B=Math.sqrt(B*B+j*j)}return i.distance=Math.floor(Math.round(B/n)*n)},getDistanceSimple:function(e,t,r){var s=i.getKeys(e),o=s.latitude,u=s.longitude;r=Math.floor(r)||1;var a={},f={};a[o]=parseFloat(i.useDecimal(e[o])).toRad(),a[u]=parseFloat(i.useDecimal(e[u])).toRad(),f[o]=parseFloat(i.useDecimal(t[o])).toRad(),f[u]=parseFloat(i.useDecimal(t[u])).toRad();var l=Math.round(Math.acos(Math.sin(f[o])*Math.sin(a[o])+Math.cos(f[o])*Math.cos(a[o])*Math.cos(a[u]-f[u]))*n);return i.distance=Math.floor(Math.round(l/r)*r)},getCenter:function(e){if(!e.length)return!1;var t=i.getKeys(e[0]),n=t.latitude,r=t.longitude,s=function(e){return Math.max.apply(Math,e)},o=function(e){return Math.min.apply(Math,e)},u,a,f={lat:[],lng:[]};for(var l in e)f.lat.push(i.useDecimal(e[l][n])),f.lng.push(i.useDecimal(e[l][r]));var c=o(f.lat),h=o(f.lng),p=s(f.lat),d=s(f.lng);u=((c+p)/2).toFixed(6),a=((h+d)/2).toFixed(6);var v=i.convertUnit("km",i.getDistance({lat:c,lng:h},{lat:p,lng:d}));return{latitude:u,longitude:a,distance:v}},getBounds:function(e){if(!e.length)return!1;var t=i.getKeys(e[0]),n=t.latitude,r=t.longitude,s=t.elevation,o=e[0].hasOwnProperty(s),u={maxLat:0,minLat:Infinity,maxLng:0,minLng:Infinity};o&&(u.maxElev=0,u.minElev=Infinity);for(var a=0,f=e.length;a<f;++a)u.maxLat=Math.max(e[a][n],u.maxLat),u.minLat=Math.min(e[a][n],u.minLat),u.maxLng=Math.max(e[a][r],u.maxLng),u.minLng=Math.min(e[a][r],u.minLng),o&&(u.maxElev=Math.max(e[a][s],u.maxElev),u.minElev=Math.min(e[a][s],u.minElev));return u},isPointInside:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude;for(var o=!1,u=-1,a=t.length,f=a-1;++u<a;f=u)(t[u][s]<=e[s]&&e[s]<t[f][s]||t[f][s]<=e[s]&&e[s]<t[u][s])&&e[r]<(t[f][r]-t[u][r])*(e[s]-t[u][s])/(t[f][s]-t[u][s])+t[u][r]&&(o=!o);return o},isPointInCircle:function(e,t,n){return i.getDistance(e,t)<n},getRhumbLineBearing:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude,o=i.useDecimal(t[s]).toRad()-i.useDecimal(e[s]).toRad(),u=Math.log(Math.tan(i.useDecimal(t[r]).toRad()/2+Math.PI/4)/Math.tan(i.useDecimal(e[r]).toRad()/2+Math.PI/4));return Math.abs(o)>Math.PI&&(o>0?o=(2*Math.PI-o)*-1:o=2*Math.PI+o),(Math.atan2(o,u).toDeg()+360)%360},getBearing:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude;t[r]=i.useDecimal(t[r]),t[s]=i.useDecimal(t[s]),e[r]=i.useDecimal(e[r]),e[s]=i.useDecimal(e[s]);var o=(Math.atan2(Math.sin(t[s].toRad()-e[s].toRad())*Math.cos(t[r].toRad()),Math.cos(e[r].toRad())*Math.sin(t[r].toRad())-Math.sin(e[r].toRad())*Math.cos(t[r].toRad())*Math.cos(t[s].toRad()-e[s].toRad())).toDeg()+360)%360;return o},getCompassDirection:function(e,t,n){var r,s;n=="circle"?s=i.getBearing(e,t):s=i.getRhumbLineBearing(e,t);switch(Math.round(s/22.5)){case 1:r={exact:"NNE",rough:"N"};break;case 2:r={exact:"NE",rough:"N"};break;case 3:r={exact:"ENE",rough:"E"};break;case 4:r={exact:"E",rough:"E"};break;case 5:r={exact:"ESE",rough:"E"};break;case 6:r={exact:"SE",rough:"E"};break;case 7:r={exact:"SSE",rough:"S"};break;case 8:r={exact:"S",rough:"S"};break;case 9:r={exact:"SSW",rough:"S"};break;case 10:r={exact:"SW",rough:"S"};break;case 11:r={exact:"WSW",rough:"W"};break;case 12:r={exact:"W",rough:"W"};break;case 13:r={exact:"WNW",rough:"W"};break;case 14:r={exact:"NW",rough:"W"};break;case 15:r={exact:"NNW",rough:"N"};break;default:r={exact:"N",rough:"N"}}return r},orderByDistance:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude,o=[];for(var u in t){var a=i.getDistance(e,t[u]);o.push({key:u,latitude:t[u][r],longitude:t[u][s],distance:a})}return o.sort(function(e,t){return e.distance-t.distance})},findNearest:function(e,t,n){n=n||0;var r=i.orderByDistance(e,t);return r[n]},getPathLength:function(e){var t=0,n;for(var r=0,s=e.length;r<s;++r)n&&(t+=i.getDistance(e[r],n)),n=e[r];return t},convertUnit:function(e,t,n){if(t===0||typeof t=="undefined"){if(i.distance===0)return 0;t=i.distance}e=e||"m",n=null==n?4:n;switch(e){case"m":return i.round(t,n);case"km":return i.round(t/1e3,n);case"cm":return i.round(t*100,n);case"mm":return i.round(t*1e3,n);case"mi":return i.round(t*(1/1609.344),n);case"sm":return i.round(t*(1/1852.216),n);case"ft":return i.round(t*(100/30.48),n);case"in":return i.round(t*100/2.54,n);case"yd":return i.round(t*(1/.9144),n)}return t},useDecimal:function(e){e=e.toString().replace(/\s*/,"");if(!isNaN(parseFloat(e))&&parseFloat(e).toString()==e)return parseFloat(e);if(i.isSexagesimal(e)===!0)return parseFloat(i.sexagesimal2decimal(e));throw"Unknown format."},decimal2sexagesimal:function(e){if(e in i.sexagesimal)return i.sexagesimal[e];var t=e.toString().split("."),n=Math.abs(t[0]),r=("0."+t[1])*60,s=r.toString().split(".");return r=Math.floor(r),s=(("0."+s[1])*60).toFixed(2),i.sexagesimal[e]=n+"° "+r+"' "+s+'"',i.sexagesimal[e]},sexagesimal2decimal:function(e){if(e in i.decimal)return i.decimal[e];var t=new RegExp(r),n=t.exec(e),s=0,o=0;n&&(s=parseFloat(n[2]/60),o=parseFloat(n[4]/3600)||0);var u=(parseFloat(n[1])+s+o).toFixed(8);return u=n[7]=="S"||n[7]=="W"?u*-1:u,i.decimal[e]=u,u},isSexagesimal:function(e){return r.test(e)},round:function(e,t){var n=Math.pow(10,t);return Math.round(e*n)/n}};typeof Number.prototype.toRad=="undefined"&&(Number.prototype.toRad=function(){return this*Math.PI/180}),typeof Number.prototype.toDeg=="undefined"&&(Number.prototype.toDeg=function(){return this*180/Math.PI}),typeof e.navigator!="undefined"?e.geolib=i:typeof module!="undefined"&&(module.exports=i)})(this),geolib.getElevation=function(){typeof window.navigator!="undefined"?geolib.getElevationClient.apply(this,arguments):geolib.getElevationServer.apply(this,arguments)},geolib.getElevationClient=function(e,t){if(!window.google)throw new Error("Google maps api not loaded");if(e.length===0)return t(null,null);if(e.length===1)return t(new Error("getElevation requires at least 2 points."));var n=[],r=geolib.getKeys(e[0]),i=r.latitude,s=r.longitude;for(var o=0;o<e.length;o++)n.push(new google.maps.LatLng(geolib.useDecimal(e[o][i]),geolib.useDecimal(e[o][s])));var u={path:n,samples:n.length},a=new google.maps.ElevationService;a.getElevationAlongPath(u,function(n,i){geolib.elevationHandler(n,i,e,r,t)})},geolib.getElevationServer=function(e,t){if(e.length===0)return t(null,null);if(e.length===1)return t(new Error("getElevation requires at least 2 points."));var n=require("googlemaps"),r=[],i=geolib.getKeys(e[0]),s=i.latitude,o=i.longitude;for(var u=0;u<e.length;u++)r.push(geolib.useDecimal(e[u][s])+","+geolib.useDecimal(e[u][o]));n.elevationFromPath(r.join("|"),r.length,function(n,r){geolib.elevationHandler(r.results,r.status,e,i,t)})},geolib.elevationHandler=function(e,t,n,r,i){var s=[],o=r.latitude,u=r.longitude;if(t=="OK"){for(var a=0;a<e.length;a++)s.push({lat:n[a][o],lng:n[a][u],elev:e[a].elevation});i(null,s)}else i(new Error("Could not get elevation using Google's API"),elevationResult.status)},geolib.getGrade=function(e){var t=geolib.getKeys(e[0]),n=t.elevation,r=Math.abs(e[e.length-1][n]-e[0][n]),i=geolib.getPathLength(e);return Math.floor(r/i*100)},geolib.getTotalElevationGainAndLoss=function(e){var t=geolib.getKeys(e[0]),n=t.elevation,r=0,i=0;for(var s=0;s<e.length-1;s++){var o=e[s][n]-e[s+1][n];o>0?i+=o:r+=Math.abs(o)}return{gain:r,loss:i}};
+(function(e,t){var n=6378137,r=/^([0-9]{1,3})°\s*([0-9]{1,3})'\s*(([0-9]{1,3}(\.([0-9]{1,2}))?)"\s*)?([NEOSW]?)$/,i={decimal:{},sexagesimal:{},distance:0,getKeys:function(e){var t=e.hasOwnProperty("lat")?"lat":"latitude",n=(e.hasOwnProperty("lng")?"lng":!1)||(e.hasOwnProperty("long")?"long":!1)||"longitude",r=(e.hasOwnProperty("alt")?"alt":!1)||(e.hasOwnProperty("altitude")?"altitude":!1)||(e.hasOwnProperty("elev")?"elev":!1)||"elevation";return{latitude:t,longitude:n,elevation:r}},getDistance:function(e,t,n){var r=i.getKeys(e),s=r.latitude,o=r.longitude,u=r.elevation;n=Math.floor(n)||1;var a={},f={};a[s]=i.useDecimal(e[s]),a[o]=i.useDecimal(e[o]),f[s]=i.useDecimal(t[s]),f[o]=i.useDecimal(t[o]);var l=6378137,c=6356752.314245,h=1/298.257223563,p=(f[o]-a[o]).toRad(),d,v,m,g,y,b,w=Math.atan((1-h)*Math.tan(parseFloat(a[s]).toRad())),E=Math.atan((1-h)*Math.tan(parseFloat(f[s]).toRad())),S=Math.sin(w),x=Math.cos(w),T=Math.sin(E),N=Math.cos(E),C=p,k,L=100;do{var A=Math.sin(C),O=Math.cos(C);b=Math.sqrt(N*A*N*A+(x*T-S*N*O)*(x*T-S*N*O));if(b===0)return i.distance=0;d=S*T+x*N*O,v=Math.atan2(b,d),m=x*N*A/b,g=1-m*m,y=d-2*S*T/g,isNaN(y)&&(y=0);var M=h/16*g*(4+h*(4-3*g));k=C,C=p+(1-M)*h*m*(v+M*b*(y+M*d*(-1+2*y*y)))}while(Math.abs(C-k)>1e-12&&--L>0);if(L===0)return NaN;var _=g*(l*l-c*c)/(c*c),D=1+_/16384*(4096+_*(-768+_*(320-175*_))),P=_/1024*(256+_*(-128+_*(74-47*_))),H=P*b*(y+P/4*(d*(-1+2*y*y)-P/6*y*(-3+4*b*b)*(-3+4*y*y))),B=c*D*(v-H);B=B.toFixed(3);if(e.hasOwnProperty(u)&&t.hasOwnProperty(u)){var j=Math.abs(e[u]-t[u]);B=Math.sqrt(B*B+j*j)}return i.distance=Math.floor(Math.round(B/n)*n)},getDistanceSimple:function(e,t,r){var s=i.getKeys(e),o=s.latitude,u=s.longitude;r=Math.floor(r)||1;var a={},f={};a[o]=parseFloat(i.useDecimal(e[o])).toRad(),a[u]=parseFloat(i.useDecimal(e[u])).toRad(),f[o]=parseFloat(i.useDecimal(t[o])).toRad(),f[u]=parseFloat(i.useDecimal(t[u])).toRad();var l=Math.round(Math.acos(Math.sin(f[o])*Math.sin(a[o])+Math.cos(f[o])*Math.cos(a[o])*Math.cos(a[u]-f[u]))*n);return i.distance=Math.floor(Math.round(l/r)*r)},getCenter:function(e){if(!e.length)return!1;var t=i.getKeys(e[0]),n=t.latitude,r=t.longitude,s=function(e){return Math.max.apply(Math,e)},o=function(e){return Math.min.apply(Math,e)},u,a,f={lat:[],lng:[]};for(var l in e)f.lat.push(i.useDecimal(e[l][n])),f.lng.push(i.useDecimal(e[l][r]));var c=o(f.lat),h=o(f.lng),p=s(f.lat),d=s(f.lng);u=((c+p)/2).toFixed(6),a=((h+d)/2).toFixed(6);var v=i.convertUnit("km",i.getDistance({lat:c,lng:h},{lat:p,lng:d}));return{latitude:u,longitude:a,distance:v}},getBounds:function(e){if(!e.length)return!1;var t=i.getKeys(e[0]),n=t.latitude,r=t.longitude,s=t.elevation,o=e[0].hasOwnProperty(s),u={maxLat:0,minLat:Infinity,maxLng:0,minLng:Infinity};o&&(u.maxElev=0,u.minElev=Infinity);for(var a=0,f=e.length;a<f;++a)u.maxLat=Math.max(e[a][n],u.maxLat),u.minLat=Math.min(e[a][n],u.minLat),u.maxLng=Math.max(e[a][r],u.maxLng),u.minLng=Math.min(e[a][r],u.minLng),o&&(u.maxElev=Math.max(e[a][s],u.maxElev),u.minElev=Math.min(e[a][s],u.minElev));return u},isPointInside:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude;for(var o=!1,u=-1,a=t.length,f=a-1;++u<a;f=u)(t[u][s]<=e[s]&&e[s]<t[f][s]||t[f][s]<=e[s]&&e[s]<t[u][s])&&e[r]<(t[f][r]-t[u][r])*(e[s]-t[u][s])/(t[f][s]-t[u][s])+t[u][r]&&(o=!o);return o},isPointInCircle:function(e,t,n){return i.getDistance(e,t)<n},getRhumbLineBearing:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude,o=i.useDecimal(t[s]).toRad()-i.useDecimal(e[s]).toRad(),u=Math.log(Math.tan(i.useDecimal(t[r]).toRad()/2+Math.PI/4)/Math.tan(i.useDecimal(e[r]).toRad()/2+Math.PI/4));return Math.abs(o)>Math.PI&&(o>0?o=(2*Math.PI-o)*-1:o=2*Math.PI+o),(Math.atan2(o,u).toDeg()+360)%360},getBearing:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude;t[r]=i.useDecimal(t[r]),t[s]=i.useDecimal(t[s]),e[r]=i.useDecimal(e[r]),e[s]=i.useDecimal(e[s]);var o=(Math.atan2(Math.sin(t[s].toRad()-e[s].toRad())*Math.cos(t[r].toRad()),Math.cos(e[r].toRad())*Math.sin(t[r].toRad())-Math.sin(e[r].toRad())*Math.cos(t[r].toRad())*Math.cos(t[s].toRad()-e[s].toRad())).toDeg()+360)%360;return o},getCompassDirection:function(e,t,n){var r,s;n=="circle"?s=i.getBearing(e,t):s=i.getRhumbLineBearing(e,t);switch(Math.round(s/22.5)){case 1:r={exact:"NNE",rough:"N"};break;case 2:r={exact:"NE",rough:"N"};break;case 3:r={exact:"ENE",rough:"E"};break;case 4:r={exact:"E",rough:"E"};break;case 5:r={exact:"ESE",rough:"E"};break;case 6:r={exact:"SE",rough:"E"};break;case 7:r={exact:"SSE",rough:"S"};break;case 8:r={exact:"S",rough:"S"};break;case 9:r={exact:"SSW",rough:"S"};break;case 10:r={exact:"SW",rough:"S"};break;case 11:r={exact:"WSW",rough:"W"};break;case 12:r={exact:"W",rough:"W"};break;case 13:r={exact:"WNW",rough:"W"};break;case 14:r={exact:"NW",rough:"W"};break;case 15:r={exact:"NNW",rough:"N"};break;default:r={exact:"N",rough:"N"}}return r},orderByDistance:function(e,t){var n=i.getKeys(e),r=n.latitude,s=n.longitude,o=[];for(var u in t){var a=i.getDistance(e,t[u]);o.push({key:u,latitude:t[u][r],longitude:t[u][s],distance:a})}return o.sort(function(e,t){return e.distance-t.distance})},findNearest:function(e,t,n){n=n||0;var r=i.orderByDistance(e,t);return r[n]},getPathLength:function(e){var t=0,n;for(var r=0,s=e.length;r<s;++r)n&&(t+=i.getDistance(e[r],n)),n=e[r];return t},convertUnit:function(e,t,n){if(t===0||typeof t=="undefined"){if(i.distance===0)return 0;t=i.distance}e=e||"m",n=null==n?4:n;switch(e){case"m":return i.round(t,n);case"km":return i.round(t/1e3,n);case"cm":return i.round(t*100,n);case"mm":return i.round(t*1e3,n);case"mi":return i.round(t*(1/1609.344),n);case"sm":return i.round(t*(1/1852.216),n);case"ft":return i.round(t*(100/30.48),n);case"in":return i.round(t*100/2.54,n);case"yd":return i.round(t*(1/.9144),n)}return t},useDecimal:function(e){e=e.toString().replace(/\s*/,"");if(!isNaN(parseFloat(e))&&parseFloat(e).toString()==e)return parseFloat(e);if(i.isSexagesimal(e)===!0)return parseFloat(i.sexagesimal2decimal(e));throw"Unknown format."},decimal2sexagesimal:function(e){if(e in i.sexagesimal)return i.sexagesimal[e];var t=e.toString().split("."),n=Math.abs(t[0]),r=("0."+t[1])*60,s=r.toString().split(".");return r=Math.floor(r),s=(("0."+s[1])*60).toFixed(2),i.sexagesimal[e]=n+"° "+r+"' "+s+'"',i.sexagesimal[e]},sexagesimal2decimal:function(e){if(e in i.decimal)return i.decimal[e];var t=new RegExp(r),n=t.exec(e),s=0,o=0;n&&(s=parseFloat(n[2]/60),o=parseFloat(n[4]/3600)||0);var u=(parseFloat(n[1])+s+o).toFixed(8);return u=n[7]=="S"||n[7]=="W"?u*-1:u,i.decimal[e]=u,u},isSexagesimal:function(e){return r.test(e)},round:function(e,t){var n=Math.pow(10,t);return Math.round(e*n)/n}};typeof Number.prototype.toRad=="undefined"&&(Number.prototype.toRad=function(){return this*Math.PI/180}),typeof Number.prototype.toDeg=="undefined"&&(Number.prototype.toDeg=function(){return this*180/Math.PI}),e.geolib=i,typeof module!="undefined"&&(module.exports=i)})(this),function(e){var t=e.geolib;t.getElevation=function(){typeof window.navigator!="undefined"?t.getElevationClient.apply(this,arguments):t.getElevationServer.apply(this,arguments)},t.getElevationClient=function(e,n){if(!window.google)throw new Error("Google maps api not loaded");if(e.length===0)return n(null,null);if(e.length===1)return n(new Error("getElevation requires at least 2 points."));var r=[],i=t.getKeys(e[0]),s=i.latitude,o=i.longitude;for(var u=0;u<e.length;u++)r.push(new google.maps.LatLng(t.useDecimal(e[u][s]),t.useDecimal(e[u][o])));var a={path:r,samples:r.length},f=new google.maps.ElevationService;f.getElevationAlongPath(a,function(r,s){t.elevationHandler(r,s,e,i,n)})},t.getElevationServer=function(e,n){if(e.length===0)return n(null,null);if(e.length===1)return n(new Error("getElevation requires at least 2 points."));var r=require("googlemaps"),i=[],s=t.getKeys(e[0]),o=s.latitude,u=s.longitude;for(var a=0;a<e.length;a++)i.push(t.useDecimal(e[a][o])+","+t.useDecimal(e[a][u]));r.elevationFromPath(i.join("|"),i.length,function(r,i){t.elevationHandler(i.results,i.status,e,s,n)})},t.elevationHandler=function(e,t,n,r,i){var s=[],o=r.latitude,u=r.longitude;if(t=="OK"){for(var a=0;a<e.length;a++)s.push({lat:n[a][o],lng:n[a][u],elev:e[a].elevation});i(null,s)}else i(new Error("Could not get elevation using Google's API"),elevationResult.status)},t.getGrade=function(e){var n=t.getKeys(e[0]),r=n.elevation,i=Math.abs(e[e.length-1][r]-e[0][r]),s=t.getPathLength(e);return Math.floor(i/s*100)},t.getTotalElevationGainAndLoss=function(e){var n=t.getKeys(e[0]),r=n.elevation,i=0,s=0;for(var o=0;o<e.length-1;o++){var u=e[o][r]-e[o+1][r];u>0?s+=u:i+=Math.abs(u)}return{gain:i,loss:s}}}(this);
View
2  package.json
@@ -33,6 +33,6 @@
"scripts": {
"test": "grunt travis --verbose"
},
- "version": "1.2.2",
+ "version": "1.2.3",
"main": "./geolib"
}
View
266 src/geolib.elevation.js
@@ -1,135 +1,141 @@
-/* Optional elevation addon requires Googlemaps API JS */
-
-/*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);
- }
-};
-
-geolib.getElevationClient = function(coords, cb) {
-
- if (!window.google) {
- throw new Error("Google maps api not loaded");
- }
-
- if (coords.length === 0) {
- return cb(null, null);
- }
-
- 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;
-
- for(var i = 0; i < coords.length; i++) {
- path.push(new google.maps.LatLng(
- geolib.useDecimal(coords[i][latitude]),
- geolib.useDecimal(coords[i][longitude])
- ));
- }
-
- var positionalRequest = {
- 'path': path,
- 'samples': path.length
+(function(global) {
+
+ var geolib = global.geolib;
+
+ /* Optional elevation addon requires Googlemaps API JS */
+
+ /*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);
+ }
+ };
+
+ geolib.getElevationClient = function(coords, cb) {
+
+ if (!window.google) {
+ throw new Error("Google maps api not loaded");
+ }
+
+ if (coords.length === 0) {
+ return cb(null, null);
+ }
+
+ 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;
+
+ for(var i = 0; i < coords.length; i++) {
+ path.push(new google.maps.LatLng(
+ geolib.useDecimal(coords[i][latitude]),
+ geolib.useDecimal(coords[i][longitude])
+ ));
+ }
+
+ 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);
+ });
+
};
- var elevationService = new google.maps.ElevationService();
- elevationService.getElevationAlongPath(positionalRequest,function (results, status) {
- geolib.elevationHandler(results, status, coords, keys, cb);
- });
-
-};
-
-geolib.getElevationServer = function(coords, cb) {
-
- if (coords.length === 0) {
- return cb(null, null);
- }
-
- 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;
-
- 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
- });
+
+ geolib.getElevationServer = function(coords, cb) {
+
+ if (coords.length === 0) {
+ return cb(null, null);
+ }
+
+ 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;
+
+ for(var i = 0; i < coords.length; i++) {
+ path.push(geolib.useDecimal(coords[i][latitude]) + ',' +
+ geolib.useDecimal(coords[i][longitude]));
}
- 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);
-};
-
-/**
-* @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;
+
+ 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);
+ }
+ };
+
+ /**
+ * @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
+ return {
+ "gain": gain,
+ "loss": loss
+ };
};
-};
+
+})(this);
View
7 src/geolib.js
@@ -798,11 +798,8 @@
}
// 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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.