How to find a distance between two geopoints in node.js? - node.js
How to find a distance between two geopoints(set of langitude and longitude) using node.js.
I have code in client side javascript using google maps distance matrix service.
I want to do same thing in serversidejavascript(In node.js router.js or datamodel.js).
My Client Side Javascript code:
function calculateDistances() {
var gs =require('googlemaps');
var latlong = { 'latitude': 52.5112, 'longitude': 13.45155};
var origin1 = gs.LatLng(13.125511084202,80.029651635576);
var origin2 = new google.maps.LatLng(12.9898593006481,80.2497744326417);;
var destinationA = new google.maps.LatLng(13.125511084202,80.029651635576);;
var destinationB = new google.maps.LatLng(12.9898593006481,80.2497744326417);
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = '';
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
addMarker(destinations[j], true);
outputDiv += origins[i] + ' to ' + destinations[j]
+ ': ' + results[j].distance.text + ' in '
+ results[j].duration.text + '';
}
}
console.log(outputDiv);
}
}
Can u suggest me to do samething in node.js package with distance matrix sample code.
Maybe this package? https://github.com/manuelbieh/geolib
And this method:
geolib.getDistance(object start, object end, [int accuracy])
Check out https://github.com/edwlook/node-google-distance
var distance = require('google-distance');
distance.get(
{
origin: 'San Francisco, CA',
destination: 'San Diego, CA'
},
function(err, data) {
if (err) return console.log(err);
console.log(data);
});
This will output:
{
index: null,
distance: '807 km',
distanceValue: 807366,
duration: '7 hours 30 mins',
durationValue: 26981,
origin: 'San Francisco, CA, USA',
destination: 'San Diego, CA, USA',
mode: 'driving',
units: 'metric',
language: 'en',
avoid: null,
sensor: false
}
Related
RecordRTC upload video to node js server
I am using RecordRTC from recording webrtc meeting. After implementing recording, when I test this application if both client are on the same system then its working fine. When I test this application on different system it isn't working fine and meeting is not recorded. Here this is my code from stop recording client side. recordRTC.stopRecording(function (videoURL) { console.log('recordRTC.stopRecording Function inside'); SelectedFile = recordRTC.getBlob(); $('#uploadForm').append('#attachmentFileId', recordRTC.getBlob()); StartUpload(); }); var FReader; var Name = "Meeting" + "_" + Date.now() + ".webm"; function StartUpload() { FReader = new FileReader(); FReader.onload = function (evnt) { socket.emit('Upload', { 'Name': Name, Data: evnt.target.result }); } socket.emit('Start', { 'Name': Name, 'Size': SelectedFile.size }); } socket.on('MoreData', function (data) { var Place = data['Place'] * 524288; //The Next Blocks Starting Position var NewFile; //The Variable that will hold the new Block of Data if (SelectedFile.webkitSlice) NewFile = SelectedFile.webkitSlice(Place, Place + Math.min(524288, (SelectedFile.size - Place))); else NewFile = SelectedFile.slice(Place, Place + Math.min(524288, (SelectedFile.size - Place))); FReader.readAsBinaryString(NewFile); }); Server Side Code I get this from here. socket.on('Start', function (data) { //data contains the variables that we passed through in the html file var Name = data['Name']; Files[Name] = { //Create a new Entry in The Files Variable FileSize : data['Size'], Data : "", Downloaded : 0 } var Place = 0; try{ var Stat = fs.statSync('Temp/' + Name); if(Stat.isFile()) { Files[Name]['Downloaded'] = Stat.size; Place = Stat.size / 524288; } } catch(er){} //It's a New File fs.open("Temp/" + Name, 'a', 0755, function(err, fd){ if(err) { console.log(err); } else { Files[Name]['Handler'] = fd; //We store the file handler so we can write to it later socket.emit('MoreData', { 'Place' : Place, Percent : 0 }); } }); }); socket.on('Upload', function (data){ var Name = data['Name']; Files[Name]['Downloaded'] += data['Data'].length; Files[Name]['Data'] += data['Data']; if(Files[Name]['Downloaded'] == Files[Name]['FileSize']) //If File is Fully Uploaded { fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){ var input = fs.createReadStream("Temp/" + Name); var output = fs.createWriteStream("Video/" + Name); //util.pump(readableStream, writableStream, [callback]) //Deprecated: Use readableStream.pipe(writableStream) input.pipe(output); input.on("end", function() { console.log("end"); fs.unlink("Temp/" + Name, function () { //This Deletes The Temporary File console.log("unlink this file:",Name ); //socket.emit('Done', {'Image' : 'Video/' + Name + '.jpg'}); }); }); }); } else if(Files[Name]['Data'].length > 10485760){ //If the Data Buffer reaches 10MB fs.write(Files[Name]['Handler'], Files[Name]['Data'], null, 'Binary', function(err, Writen){ Files[Name]['Data'] = ""; //Reset The Buffer var Place = Files[Name]['Downloaded'] / 524288; var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100; socket.emit('MoreData', { 'Place' : Place, 'Percent' : Percent}); }); } else { var Place = Files[Name]['Downloaded'] / 524288; var Percent = (Files[Name]['Downloaded'] / Files[Name]['FileSize']) * 100; socket.emit('MoreData', { 'Place' : Place, 'Percent' : Percent}); } }); If both clients are on same machine/system its working fine, but if both clients are on different system then meeting is not recorded.
-FIXED- cannot get / express and node js
hey guys im working on a project with some friends and we want our server on openshift it runs without errors but it always gives cannot get / i tried to look for solutions and tried many but it just doesent fix it. any ideas ? var express = require('express'); var app = express(); var server = require('http').Server(app); var io = require('socket.io').listen(server); // app.use(express.static(__dirname + '/public')); app.use('/static', express.static(__dirname + '/public')); server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP); io.on('connection', onSocketConnection); var players = []; var npc1 = new Player(1049, 980); npc1.id = "Johan Kruijs"; npc1.color = "gold"; npc1.name = "[NPC] Johan Kruijs"; players.push(npc1); function onSocketConnection(client) { console.log("Player has connected: "); client.on("disconnect", onClientDisconnect); client.on("new player", onNewPlayer); client.on("move player", onMovePlayer); }; function onClientDisconnect() { var removePlayer = playerById(this.id); if (!removePlayer) { console.log("Player not found: " + this.id); return; } console.log(removePlayer.name + " has disconnected."); players.splice(players.indexOf(removePlayer), 1); this.broadcast.emit("remove player", { id: this.id }); }; function onNewPlayer(data) { var newPlayer = new Player(data.x, data.y); newPlayer.id = this.id; newPlayer.name = data.name; newPlayer.color = data.color; this.broadcast.emit("new player", { id: newPlayer.id, x: newPlayer.x, y: newPlayer.y, name: newPlayer.name, color: newPlayer.color }); var i, existingPlayer; for (i = 0; i < players.length; i++) { existingPlayer = players[i]; this.emit("new player", { id: existingPlayer.id, x: existingPlayer.x, y: existingPlayer.y, name: existingPlayer.name, color: existingPlayer.color }); }; players.push(newPlayer); console.log(" - name: [" + newPlayer.name + "]") console.log(" - id: [" + newPlayer.id + "]"); }; function onMovePlayer(data) { var player = playerById(data.id); player.x = data.x; player.y = data.y; player.id = data.id; io.emit("move player", { id: data.id, x: data.x, y: data.y }) }; function playerById(id) { var i; for (i = 0; i < players.length; i++) { if (players[i].id == id) return players[i]; }; return false; }; function Player(xpos, ypos) { var result = { x: xpos, y: ypos, id: 0 } return result; } path --
In the screenshot you shared, the folder name is Public and not public, in osX (I assume that is what you are using from the screenshot), Public and public are different. If you write this, app.use(express.static(__dirname + '/Public')); Things should start working. Plus if you wanna set a default page, i.e. when user visits / and you want your /index.html to be served, you can do it like app.use('/', express.static(__dirname + '/Public', {index: "index.html"})); I hope this resolves your issue!!
creating tickboxes to show/hide series in flot
I am trying to show/hide each line in graph using checkbox. The check box appears but nothing happens when i click it https://jsfiddle.net/shorif2000/2pb3yu5t/ var arrayFromPHP = { chartdata: { "2G": "[[1450623600000,99.55],[1450620000000,99.54],[1450616400000,99.51],[1450612800000,99.51],[1450609200000,99.5],[1450605600000,99.51],[1450602000000,99.52],[1450598400000,99.55],[1450594800000,99.57],[1450591200000,99.57],[1450587600000,99.58],[1450584000000,99.58],[1450580400000,99.58],[1450576800000,99.57],[1450573200000,99.54],[1450569600000,99.54],[1450566000000,99.53],[1450562400000,99.5],[1450558800000,99.49],[1450555200000,99.5],[1450551600000,99.54],[1450548000000,99.6],[1450544400000,99.6],[1450540800000,99.6],[1450537200000,99.57],[1450533600000,99.52],[1450530000000,99.55],[1450526400000,99.56],[1450522800000,99.54],[1450519200000,99.52],[1450515600000,99.55],[1450512000000,99.59],[1450508400000,99.57],[1450504800000,99.57],[1450501200000,99.57],[1450497600000,99.59],[1450494000000,99.59],[1450490400000,99.58],[1450486800000,99.58],[1450483200000,99.57],[1450479600000,99.58],[1450476000000,99.58],[1450472400000,99.57],[1450468800000,99.57],[1450465200000,99.56],[1450461600000,99.56],[1450458000000,99.55],[1450454400000,99.49],[1450450800000,99.39],[1450447200000,99.36],[1450443600000,99.28],[1450440000000,99.23],[1450436400000,99.17],[1450432800000,99.18],[1450429200000,99.28],[1450425600000,99.34],[1450422000000,99.44],[1450418400000,99.44],[1450414800000,99.46],[1450411200000,99.45],[1450407600000,99.45],[1450404000000,99.35],[1450400400000,99.36],[1450396800000,99.35]]", "2G3G": "[[1450623600000,99.53],[1450620000000,99.52],[1450616400000,99.5],[1450612800000,99.49],[1450609200000,99.5],[1450605600000,99.5],[1450602000000,99.51],[1450598400000,99.55],[1450594800000,99.58],[1450591200000,99.58],[1450587600000,99.58],[1450584000000,99.59],[1450580400000,99.58],[1450576800000,99.58],[1450573200000,99.57],[1450569600000,99.57],[1450566000000,99.54],[1450562400000,99.56],[1450558800000,99.58],[1450555200000,99.57],[1450551600000,99.59],[1450548000000,99.62],[1450544400000,99.61],[1450540800000,99.58],[1450537200000,99.57],[1450533600000,99.52],[1450530000000,99.53],[1450526400000,99.53],[1450522800000,99.49],[1450519200000,99.5],[1450515600000,99.53],[1450512000000,99.6],[1450508400000,99.6],[1450504800000,99.6],[1450501200000,99.6],[1450497600000,99.6],[1450494000000,99.6],[1450490400000,99.6],[1450486800000,99.6],[1450483200000,99.6],[1450479600000,99.6],[1450476000000,99.6],[1450472400000,99.58],[1450468800000,99.56],[1450465200000,99.57],[1450461600000,99.56],[1450458000000,99.56],[1450454400000,99.48],[1450450800000,99.38],[1450447200000,99.3],[1450443600000,99.25],[1450440000000,99.16],[1450436400000,99.03],[1450432800000,99.04],[1450429200000,99.14],[1450425600000,99.23],[1450422000000,99.36],[1450418400000,99.37],[1450414800000,99.38],[1450411200000,99.37],[1450407600000,99.37],[1450404000000,99.34],[1450400400000,99.34],[1450396800000,99.34]]", "2G3G4G": "[[1450623600000,99.57],[1450620000000,99.56],[1450616400000,99.55],[1450612800000,99.54],[1450609200000,99.56],[1450605600000,99.55],[1450602000000,99.56],[1450598400000,99.59],[1450594800000,99.62],[1450591200000,99.62],[1450587600000,99.63],[1450584000000,99.63],[1450580400000,99.62],[1450576800000,99.62],[1450573200000,99.62],[1450569600000,99.62],[1450566000000,99.59],[1450562400000,99.61],[1450558800000,99.62],[1450555200000,99.62],[1450551600000,99.63],[1450548000000,99.65],[1450544400000,99.65],[1450540800000,99.62],[1450537200000,99.61],[1450533600000,99.55],[1450530000000,99.56],[1450526400000,99.56],[1450522800000,99.53],[1450519200000,99.54],[1450515600000,99.57],[1450512000000,99.64],[1450508400000,99.64],[1450504800000,99.64],[1450501200000,99.64],[1450497600000,99.64],[1450494000000,99.64],[1450490400000,99.63],[1450486800000,99.63],[1450483200000,99.63],[1450479600000,99.62],[1450476000000,99.62],[1450472400000,99.61],[1450468800000,99.6],[1450465200000,99.6],[1450461600000,99.59],[1450458000000,99.59],[1450454400000,99.52],[1450450800000,99.43],[1450447200000,99.34],[1450443600000,99.3],[1450440000000,99.2],[1450436400000,99.07],[1450432800000,99.08],[1450429200000,99.19],[1450425600000,99.28],[1450422000000,99.41],[1450418400000,99.42],[1450414800000,99.42],[1450411200000,99.42],[1450407600000,99.42],[1450404000000,99.39],[1450400400000,99.39],[1450396800000,99.4]]", "3G": "[[1450623600000,99.51],[1450620000000,99.52],[1450616400000,99.5],[1450612800000,99.48],[1450609200000,99.5],[1450605600000,99.49],[1450602000000,99.5],[1450598400000,99.54],[1450594800000,99.58],[1450591200000,99.58],[1450587600000,99.58],[1450584000000,99.59],[1450580400000,99.57],[1450576800000,99.58],[1450573200000,99.58],[1450569600000,99.58],[1450566000000,99.55],[1450562400000,99.58],[1450558800000,99.61],[1450555200000,99.6],[1450551600000,99.61],[1450548000000,99.63],[1450544400000,99.61],[1450540800000,99.58],[1450537200000,99.57],[1450533600000,99.51],[1450530000000,99.52],[1450526400000,99.52],[1450522800000,99.48],[1450519200000,99.49],[1450515600000,99.52],[1450512000000,99.6],[1450508400000,99.62],[1450504800000,99.62],[1450501200000,99.61],[1450497600000,99.61],[1450494000000,99.61],[1450490400000,99.61],[1450486800000,99.61],[1450483200000,99.61],[1450479600000,99.6],[1450476000000,99.6],[1450472400000,99.58],[1450468800000,99.56],[1450465200000,99.57],[1450461600000,99.56],[1450458000000,99.56],[1450454400000,99.48],[1450450800000,99.38],[1450447200000,99.28],[1450443600000,99.24],[1450440000000,99.13],[1450436400000,98.98],[1450432800000,98.99],[1450429200000,99.09],[1450425600000,99.19],[1450422000000,99.33],[1450418400000,99.34],[1450414800000,99.35],[1450411200000,99.34],[1450407600000,99.34],[1450404000000,99.33],[1450400400000,99.33],[1450396800000,99.33]]", "4G": "[[1450623600000,99.89],[1450620000000,99.84],[1450616400000,99.87],[1450612800000,99.89],[1450609200000,99.95],[1450605600000,99.95],[1450602000000,99.96],[1450598400000,99.95],[1450594800000,99.97],[1450591200000,99.96],[1450587600000,99.96],[1450584000000,99.97],[1450580400000,99.97],[1450576800000,99.98],[1450573200000,99.99],[1450569600000,99.98],[1450566000000,99.97],[1450562400000,99.97],[1450558800000,99.96],[1450555200000,99.96],[1450551600000,99.95],[1450548000000,99.92],[1450544400000,99.92],[1450540800000,99.9],[1450537200000,99.89],[1450533600000,99.84],[1450530000000,99.81],[1450526400000,99.84],[1450522800000,99.82],[1450519200000,99.81],[1450515600000,99.83],[1450512000000,99.93],[1450508400000,99.92],[1450504800000,99.92],[1450501200000,99.92],[1450497600000,99.92],[1450494000000,99.92],[1450490400000,99.85],[1450486800000,99.86],[1450483200000,99.86],[1450479600000,99.84],[1450476000000,99.83],[1450472400000,99.88],[1450468800000,99.85],[1450465200000,99.84],[1450461600000,99.84],[1450458000000,99.84],[1450454400000,99.8],[1450450800000,99.75],[1450447200000,99.67],[1450443600000,99.64],[1450440000000,99.51],[1450436400000,99.34],[1450432800000,99.36],[1450429200000,99.56],[1450425600000,99.69],[1450422000000,99.79],[1450418400000,99.78],[1450414800000,99.78],[1450411200000,99.77],[1450407600000,99.78],[1450404000000,99.77],[1450400400000,99.79],[1450396800000,99.87]]" }, data_rows: 64, days: "3", days_per_period: 1, days_per_period_unavailability: 1, filter: "", filtersql: "", filtersql2: " AND crqcoos is null AND crqinflight is null", hours: 1, hours_unavailability: 1, max_days: 30, min: 98, period: "hh24", tick: 6, tick_type: "hour", timeformat: "%a<br>%H:%M %p<br>%d %b" }; var datasets = {}; choices_CAGraph(arrayFromPHP); function choices_CAGraph(arrayFromPHP) { //initial load //var datasets = []; $.each(arrayFromPHP.chartdata, function(i, elem) { var jsonObj = $.parseJSON('[' + elem + ']'); var iLabel = i; datasets[i.toLowerCase()] = { label: iLabel, data: jsonObj[0] }; }); var i = 0; $.each(datasets, function(key, val) { val.color = i; ++i; }); var choiceContainer_CAGraph = $("#choices_CAGraph"); $.each(datasets, function(key, val) { choiceContainer_CAGraph.append("<input class='cc' type='checkbox' name='" + key + "' checked='checked' id='id" + key + "' value='" + key + "'></input>" + "<label for='id" + key + "'>" + val.label + "</label>"); }); //choiceContainer_CAGraph.find("input").click(plotAccordingToChoices()); var showpoints = false; if ((arrayFromPHP.data_rows == 1) && (arrayFromPHP.period == "hh24") && (arrayFromPHP.days == 1)) showpoints = true; else if ((arrayFromPHP.period == "dd" || arrayFromPHP.period == "day" || arrayFromPHP.period == "mon") && arrayFromPHP.days == 1) showpoints = true; var options = { legend: { position: "sw", noColumns: 5 }, yaxis: { min: arrayFromPHP.min, max: 100 }, xaxis: { mode: "time", timeformat: arrayFromPHP.timeformat, tickSize: [arrayFromPHP.tick, arrayFromPHP.tick_type] }, grid: { clickable: true, hoverable: true }, series: { points: { show: showpoints } } }; plotAccordingToChoices(options); } function plotAccordingToChoices(options) { var choiceContainer_CAGraph = $("#choices_CAGraph"); var data = []; choiceContainer_CAGraph.find("input:checked").each(function() { var key = $(this).attr("name"); if (key && datasets[key]) data.push(datasets[key]); }); $.plot("#CAGraph", data, options); } arrayFromPHP is json output from ajax request. I have 5 graphs on this page and I am trying to reuse this function so they all have show/hide feature. currently the tick boxes appear but nothing happens.
Change //choiceContainer_CAGraph.find("input").click(plotAccordingToChoices()); to choiceContainer_CAGraph.find("input").click(function() { plotAccordingToChoices(options); }); Updated fiddle
Missing 80% of GET responses
Our company is planning on transitioning from REDIS to Aerospike, but we are seeing some strange issues with missing get requests (only 35% making it back to the callback function). Here is the code we are testing with: var cluster = require('cluster'); var numCPUs = require('os').cpus().length; if (cluster.isMaster) { for (var i = 0; i < numCPUs; i++) { var worker = cluster.fork(); } } else { var start = new Date().getTime(); var requests = 0; var responses = 0; var aerospike = require('./node_modules/aerospike'); var status = aerospike.status; var client = aerospike.client({ hosts: [ { addr: '127.0.0.1', port: 3000 } ] }); function connect_cb( err, client) { if (err.code == status.AEROSPIKE_OK) { console.log("Aerospike Connection Success") } } client.connect(connect_cb) setInterval(function(){ for(var i=0; i<50; i++) { var key = aerospike.key('dexi','toys','floor_'+i); requests++; client.get(key, function(err, rec, meta) { responses++; if ( err.code == status.AEROSPIKE_OK ) { } else { console.error('Get Error:', err); } }); } },10); setInterval(function(){ for(var i=0; i<50; i++) { var key = aerospike.key('dexi','toys','floor_'+i); var rec = { uid: 1000, // integer data stored in bin called "uid" name: "user_name", // string data stored in bin called "user_name" dob: { mm: 12, dd: 29, yy: 1995}, // map data stored (msgpack format) in bin called "dob" friends: [1001, 1002, 1003] }; var metadata = { ttl: 10000, gen: 0 }; client.put(key, rec, metadata, function(err) { switch ( err.code ) { case status.AEROSPIKE_OK: break; default: console.error("Put Error: " + err.message); exitCode = 1; break; } }); } },10); setInterval(function(){ var timeSpent = ( new Date().getTime()) - start; console.log(requests, responses,timeSpent); },15000); } Below is the console output we are seeing: 34400 9306 15098 34150 9250 15080 35050 9330 15087 34150 9235 15092 33250 9310 15120 33950 9249 15090 34650 9298 15101 35000 9400 15102 34700 9300 15166 33150 9399 15181 34500 9300 15193 33850 9292 15207 34400 9250 15162 34100 9360 15212 34050 9250 15171 34100 9348 15159 33800 9250 15118 34300 9309 15189 34050 9300 15152 34250 9405 15181 As you can see, on average, for every 35k get requests we send, we are only seeing a small % of them actually come back. Our Aerospike dashboard also reflects the discrepancy (only seeing 35% of the gets sent), as the throughput is reflecting the responses we are getting back.
Instagram real time tag subscription only work for a minute - node.js
I am trying to get the real time tag subscription to work but it seems like the real time update only update within a minute. Here are my code, can you please point out what I am missing? var Instagram = require('instagram-node-lib'); app.get('/GetInstagram', function(request, response){ // The GET callback for each subscription verification. var params = url.parse(request.url, true).query; response.send(params['hub.challenge'] || 'No hub.challenge present'); }); app.post('/GetInstagram', function (req, res) { var tagName = 'love'; Instagram.set('client_id', 'clientID'); Instagram.set('client_secret', 'ClientSecret'); Instagram.tags.subscribe ({ object_id: tagName,access_token: null, client_id: 'clientid', client_secret: 'clientsecret', callback_url: 'http://myURL/GetInstagram', complete: function() { console.log('begin fetching'); Instagram.tags.recent({ name: tagName,aspect: 'media', complete: function(data, pagination ) { var chunk = { 'pagination': pagination, 'data': data }; console.log (chunk.data.length); var len = 0; for (var i = 0, len = chunk.data.length; i < len; i++) { var url = '"' + chunk.data[i].images.standard_resolution.url + '"'; console.log('image from instagram image # ' + i + ' image URL' + chunk.data[i].images.standard_resolution.url ); } res.end(); } }); } }); });
pretty sure you're not using the right method -- you want to use Instagram.subscriptions.subscribe for a real-time feed, not tags.subscribe