Skip to content

Commit

Permalink
made async methods pass error through callback
Browse files Browse the repository at this point in the history
  • Loading branch information
regality committed Apr 5, 2012
1 parent e477112 commit e668c43
Showing 1 changed file with 55 additions and 48 deletions.
103 changes: 55 additions & 48 deletions geolib.js
Expand Up @@ -628,56 +628,64 @@
}, },


getElevationClient: function(coords, cb) { getElevationClient: function(coords, cb) {
if (!window.google) { try {
throw new Error("Google maps api not loaded"); if (!window.google) {
} return cb(new Error("Geolib: Google maps api not loaded"));
if (coords.length == 0) { }
return cb(null, null); if (coords.length == 0) {
} return cb(null, null);
if (coords.length == 1) { }
return cb(new Error("getElevation requires at least 2 points.")); if (coords.length == 1) {
return cb(new Error("Geolib: 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);
});
} catch (e) {
return cb(e);
} }
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);
});
}, },


getElevationServer: function(coords, cb){ getElevationServer: function(coords, cb){
if (coords.length == 0) { try {
return cb(null, null); if (coords.length == 0) {
} return cb(null, null);
if (coords.length == 1) { }
return cb(new Error("getElevation requires at least 2 points.")); if (coords.length == 1) {
return cb(new Error("Geolib: 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)
});
} catch (e) {
return cb(e);
} }
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)
});
}, },


elevationHandler: function(results, status, coords, keys, cb){ elevationHandler: function(results, status, coords, keys, cb){
Expand All @@ -694,7 +702,7 @@
} }
cb(null, latsLngsElevs); cb(null, latsLngsElevs);
} else { } else {
cb(new Error("Could not get elevation using Google's API"), elevationResult.status); cb(new Error("Geolib: Could not get elevation using Google's API"), elevationResult.status);
} }
}, },


Expand Down Expand Up @@ -747,7 +755,6 @@
if(distance == 0 || typeof distance == 'undefined') { if(distance == 0 || typeof distance == 'undefined') {


if(geolib.distance == 0) { if(geolib.distance == 0) {
// throw 'No distance given.';
return 0; return 0;
} else { } else {
distance = geolib.distance; distance = geolib.distance;
Expand Down Expand Up @@ -812,7 +819,7 @@
} else if(geolib.isSexagesimal(value) == true) { } else if(geolib.isSexagesimal(value) == true) {
return parseFloat(geolib.sexagesimal2decimal(value)); return parseFloat(geolib.sexagesimal2decimal(value));
} else { } else {
throw 'Unknown format.'; throw new Error('Geolib: Unknown format.');
} }


}, },
Expand Down

0 comments on commit e668c43

Please sign in to comment.