server/utils.js
/**
 * Create custom Matrix from multiple Distance matrix API calls, Asynchronously.
 * Since parallel calls are made to Google API, hence faster,Use this for production.
 * Warning! Due to the parallel nature, in case one of the request fails, it will still execute for remaining calls.
 * @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, Asynchronously.
Since parallel calls are made to Google API, hence faster,Use this for production.
Warning. Due to the parallel nature, in case one of the request fails, it will still execute for remaining calls.
@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 Asynchronously . Since parallel calls are made to Google API hence faster Use this for production . Warning . Due to the parallel nature in case one of the request fails it will still execute for remaining calls . @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 fillCustomMatrix(sections, customMatrix) {
  return new Promise(async (resolve, reject) => {
    try {
      let sectionsRequests = [];
      for (let i = 0; i < sections.length; i++) {
        sectionsRequests.push(
          (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];
              }
            }
          })(sections[i]));
      }
      await Promise.all(sectionsRequests);
      resolve(customMatrix);
    } catch (err) {
      reject(`fillCustomMatrix():${err}`);
    }
  });
}
async function fillCustomMatrix ( sections , customMatrix ) { return new Promise ( async ( resolve , reject ) => { try { let sectionsRequests = [ ] ; for ( let i = 0 ; i < sections . length ; i ++ ) { sectionsRequests . push ( ( 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 ] ; } } } ) ( sections [ i ] ) ) ; } await Promise . all ( sectionsRequests ) ; resolve ( customMatrix ) ; } catch ( err ) { reject ( ` ${ err } ` ) ; } } ) ; }
Create custom Matrix from multiple Distance matrix API calls, Asynchronously.
Create custom Matrix from multiple Distance matrix API calls Asynchronously .