Skip to content

Instantly share code, notes, and snippets.

@voratham
Created February 6, 2018 03:33
Show Gist options
  • Save voratham/db1945b7a423a143f2a3b0258005ad4b to your computer and use it in GitHub Desktop.
Save voratham/db1945b7a423a143f2a3b0258005ad4b to your computer and use it in GitHub Desktop.
const redis = require("redis"),
client = redis.createClient();
var myLocation = { latitude : 13.617531, longitude : 100.623084 }
var taxi1 = { name : "taxi1" , latitude : 13.627457, longitude : 100.627518}
var taxi2 = { name: "taxi2" , latitude : 13.744965, longitude : 100.638121 }
var taxi_all = [ taxi1 , taxi2 ]
console.log('taxi_all :: ' , taxi_all);
// if you'd like to select database 3, instead of 0 (default), call
// client.select(3, function() { /* ... */ });
taxi_all.forEach( taxi => {
client.geoadd('track', taxi.longitude, taxi.latitude , taxi.name , (error , data) => {
// console.log('error :: ' , error)
if(data === 1) return console.log('successfully geo added ! :: ' , taxi.name)
console.log('-------------------')
});
});
// // find taxi
console.log('---------------');
client.georadius("track", myLocation.longitude , myLocation.latitude , '100' , 'km' , 'WITHCOORD' , (error ,data) => {
// console.log('error :: ' , error)
// console.log('data :: ' ,data);
// chage array flattern
const result = data.reduce( (newArray , object) => {
console.log({ name : object[0] , latitude : object[1][1] , longitude : object[1][0]} )
} , [])
console.log('newResult ::' , result );
})
client.on("error", function (err) {
console.log("Error " + err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment