server/utils.js
/**
 * Create custom Matrix from multiple Distance matrix API calls, Synchronously.
 * Useful for Dev mode, or in case for slow computation, in case your Google Account has exceeded rate-limit 
 * Minimises wasteful calls, since the function stops when it receives failure response.
 * @param {Array({x1:number, x2:number, y1: number, y2: number}} sections, array of section of coordinateArray matrix  
 * @param {Array(Array(number))} customMatrix, empty 2-D Array for storing distance matrix responses
 * @returns {Array(Array(number))} customMatrix 
 */
Create custom Matrix from multiple Distance matrix API calls, Synchronously.
Useful for Dev mode, or in case for slow computation, in case your Google Account has exceeded rate-limit
Minimises wasteful calls, since the function stops when it receives failure response.
@param {Array({x1:number, x2:number, y1: number, y2: number}} sections, array of section of coordinateArray matrix
@param {Array(Array(number))} customMatrix, empty 2-D Array for storing distance matrix responses
@returns {Array(Array(number))} customMatrix
Create custom Matrix from multiple Distance matrix API calls Synchronously . Useful for Dev mode or in case for slow computation in case your Google Account has exceeded rate - limit Minimises wasteful calls since the function stops when it receives failure response . @param { Array ( { x1 : number x2 : number y1 : number y2 : number }} sections array of section of coordinateArray matrix @param { Array ( Array ( number )) } customMatrix empty 2 - D Array for storing distance matrix responses @returns { Array ( Array ( number )) } customMatrix
async function fillCustomMatrixSync(sections, customMatrix) {
  return new Promise(async (resolve, reject) => {
    try {
      for (let i = 0; i < sections.length; i++) {
        await (async function (section) {
          console.log(`Distance Matrix Request #${i} sent`);
          let data = await sendRequest(section.url);
          saveTofile(`DM_REQ#${i}`, data);
          let durationMatrix = await extractDurationData(data);
          for (let r = section.x1; r <= section.x2; r++) {
            for (let c = section.y1; c <= section.y2; c++) {
              customMatrix[r][c] = durationMatrix[r - section.x1][c - section.y1];
            }
          }
          await waitBuffer(200);
        })(sections[i]);
      }
      resolve(customMatrix);
    } catch (err) {
      reject(`fillCustomMatrixSync():${err}`);
    }
  });
}
async function fillCustomMatrixSync ( sections , customMatrix ) { return new Promise ( async ( resolve , reject ) => { try { for ( let i = 0 ; i < sections . length ; i ++ ) { await ( async function ( section ) { console . log ( ` ${ i } ` ) ; let data = await sendRequest ( section . url ) ; saveTofile ( ` ${ i } ` , data ) ; let durationMatrix = await extractDurationData ( data ) ; for ( let r = section . x1 ; r <= section . x2 ; r ++ ) { for ( let c = section . y1 ; c <= section . y2 ; c ++ ) { customMatrix [ r ] [ c ] = durationMatrix [ r - section . x1 ] [ c - section . y1 ] ; } } await waitBuffer ( 200 ) ; } ) ( sections [ i ] ) ; } resolve ( customMatrix ) ; } catch ( err ) { reject ( ` ${ err } ` ) ; } } ) ; }
Create custom Matrix from multiple Distance matrix API calls, Synchronously.
Create custom Matrix from multiple Distance matrix API calls Synchronously .