Skip to content

Instantly share code, notes, and snippets.

@isummation
Last active September 28, 2017 11:57
Show Gist options
  • Select an option

  • Save isummation/66b81d16cd6c0d12602d4e3864e221b1 to your computer and use it in GitHub Desktop.

Select an option

Save isummation/66b81d16cd6c0d12602d4e3864e221b1 to your computer and use it in GitHub Desktop.
const _config = require('../config');
const sql = require('mssql');
var GoogleMapService = {};
GoogleMapService.init = async function() {
try {
GoogleMapService.googleMapsClient = require('@google/maps').createClient({
key: _config.GoogleMapKey,
Promise: require("bluebird").Promise
});
GoogleMapService.pool = await new sql.ConnectionPool( _config.mssql ).connect();
let tableData = await GoogleMapService.pool.request()
.query(
`
select id, lat, lng
from tblAddress
where altitude is null
`
)
for (let obj of tableData.recordset){
let res = await GoogleMapService.googleMapsClient.elevation({
locations: {
lat: obj.lat,
lng: obj.lng
}
}).asPromise();
if (res.json.status == "OK") {
await GoogleMapService.pool.request()
.input('altitude', sql.Float, res.json.results[0].elevation )
.input('id', sql.Int, obj.propertyId)
.query(
`
UPDATE tblAddress
SET altitude = @altitude
WHERE id = @id
`
)
console.log( "tblAddress Updated", { result: res.json, property : obj } );
} else if (res.json.status == "OVER_QUERY_LIMIT") {
console.error( "Limit execeed", { result: res.json, property : obj } );
break;
} else {
console.error("Logging other errors", { result: res.json, property : obj } );
}
await GoogleMapService.sleep(2000); //sleep 2 second
}
GoogleMapService.terminate();
} catch (e) {
console.log("Google Map Service Error", e);
GoogleMapService.terminate();
}
}
GoogleMapService.terminate = function(){
GoogleMapService.pool.close();
}
GoogleMapService.sleep = function(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
GoogleMapService.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment