var work = function(start, stop) { var gps1, gps2, prefix; if (errP) return; gps1 = start.gps.split(','); gps2 = stop.gps.split(','); if (utilities.distanceFromLatLonInKm(gps1[0], gps1[1], gps2[0], gps2[1]) > range) return; prefix = JSON.stringify(start) + '/' + JSON.stringify(stop) + ' distance: '; distance.get({ origin : start.gps , destination : stop.gps , units : 'metric' }, function(err, data) { var result; if (!!err) return done(prefix + err.message); result = utilities.distanceInKm(data.distance); if (isNaN(result)) return done(prefix + data); if (result <= range) sites[start.address].distances[stop.address] = result; done(); }); count++; };
exports.route = function(origin, destination, range, callback) { var gps1, gps2; origin.distances = {}; sites.origin = origin; destination.distances = {}; sites.destination = destination; gps1 = origin.gps.split(','); gps2 = destination.gps.split(','); if (utilities.distanceFromLatLonInKm(gps1[0], gps1[1], gps2[0], gps2[1]) > range) { return prep(range, callback); } distance.get({ origin : origin.gps , destination : destination.gps , units : 'metric' }, function(err, data) { var result; if (!!err) return callback(err); result = utilities.distanceInKm(data.distance); if (isNaN(result)) return callback(new Error('origin/destination distance: ' + data)); if (result > range) return callback(null, []); prep(range, callback); }); };
router.post('/distance1', function(req, res){ distance.get( { origin: req.body.origin, destination: req.body.destination }, function(err, data) { if (err) return console.log(err); res.json(data); }); });
exports.getDistance = function(req, res) { var o = req.param('origin'); var d = req.param('destination'); distance.get({ origin : o, destination : d }, function(err, data) { res.send(data); }); };
const arrivalTime = function (message, callback) { GoogleDistance.get({ origin: message.origin, destination: '112 19th Ave S, Nashville, TN 37203' }, (err, result) => { if (err) { return callback(err) } callback(null, { time: Date.now() + (result.durationValue * 1000) }) }) }
onsendHelp: function(event, from, to, recipientId, location) { distance.get( { index: 1, origin: '50.061871, 19.939624', mode: 'walking', destination: location }, function(err, data) { if (err) return console.log(err); sender.sendMessage(recipientId, "Wolontariusz jest od Ciebie oddalony o " + data.distance + ". Dotrze do Ciebie za " + data.duration); }); },
// Gets you a point some distance away (miles) in the bearing that you desire (deg) function getDestination(lat, lon, brng, miles, duration, callback, finalCallback) { dist = miles * 1.60934; dist = dist / 6371; brng = toRad(brng); //console.log("lat: " + lat + "lon:" + lon); var lat1 = toRad(lat), lon1 = toRad(lon); var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng)); var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2)); if (isNaN(lat2) || isNaN(lon2)) return null; lat2 = toDeg(lat2); lon2 = toDeg(lon2); console.log(lat2 + ", " + lon2); /*gm.reverseGeocode(gm.checkAndConvertPoint([lat2, lon2]), function(err, data) { callback(data.results, toDeg(lat) % 360, toDeg(lon) % 360, toDeg(brng), dist * 6371 / 1.60934, finalCallback); });*/ distance.get({ origin: myLocation[0] + "," + myLocation[1], destination: lat2 + "," + lon2 }, function(err, data) { //if (err) return console.log("Error in getting distance:" + err); //console.log(data); if (!err) { if (data.durationValue > duration * 60 * 60) { console.log("Time " + data.durationValue / 3600 + " is longer than " + duration + ". Retrying with distance " + miles + " and angle " + toDeg(brng)); return getDestination(lat, lon, toDeg(brng), .7 * miles, duration, callback, finalCallback); } else { console.log("Time = " + data.durationValue / 3600); callback({ lat: lat2, lon: lon2, hours: Math.floor(data.durationValue / 1800), mins: data.durationValue % 60, cost: ((data.distanceValue * 0.000621371) / 30) * gasPriceHere, gasTanks: (((data.distanceValue * 0.000621371) / 30)) / totalGallons }, lat, lon, toDeg(brng), dist * 6371 / 1.60934, duration, finalCallback); } } else { callback(null, lat, lon, toDeg(brng), dist * 6371 / 1.60934, duration, finalCallback); } }); };
router.post('/distance', function(req, res){ var currentcity = req.user.local.currentcity; var currentstate = req.user.local.currentstate; var origin = currentcity + ", " + currentstate; distance.get( { origin: origin, destination: req.body.destination }, function(err, data) { if (err) return console.log(err); res.json(data); }); });
//this function calculates the distance from the driver to the passegner's chosesn location function getDriverBusStopDistance(){ //use the google-distance matrix API for nodejs distance.get( { origin: 'currentDriverLatitude, currentDriverLongitude', destination: 'currentBusStopLatitude, currentBusStopLongitude', mode: 'driving', units: 'metric', language: 'en', }, function(err, data) { if (err) return console.log(err); console.log(data); }); }
function check(from, to, threshold, thresholdReachedCallback) { distance.get({ origin: from, destination: to, }, function(err, data) { if (err) { console.error('Error:', err); return; } var durationInMinutes = parseInt(data.durationValue/60); console.log(durationInMinutes + ' min'); if (durationInMinutes <= threshold) { thresholdReachedCallback(durationInMinutes); } }); }
function distanceMatrixHelper( socket, userOrigin, locations, distances ) { distance.get( { origin: userOrigin.lat + ', ' + userOrigin.lng, destinations: distances }, function( err, data ) { if( err ) { console.log( 'ERRR: ' + err ); } for( var i in data ) { locations[ i ].distance = data[ i ].distanceValue; } locations.sort( function( a, b ) { return parseInt( a.distance ) - parseInt( b.distance ); }); socket.emit( 'distances', locations ); socket.on( 'distancesToggle', function( nothing ) { socket.emit( 'distanceReturn', locations ); }); }); }
'use strict'; var distance = require('google-distance'); var options = { origin: 'New York City, USA', destination: 'San Diego, USA' }; function onDistance (err, result) { if (err) { return console.error(err); } console.log(result); } distance.get(options, onDistance);