How to use the API key in my Google-app-w370 application using Maps. method?

We are starting to work with google-apps-script and Google applications in general, where I am currently working. I searched and searched on stackoverflow to try and solve this problem. I will try to explain the clearest!

So, we have a Google spreadsheet where we calculate the total distance between different points on the route. Something like that:

Google example table

So, in my google script application, I have this piece of code:

 var directionFinder = Maps.newDirectionFinder();
  directionFinder.setMode(Maps.DirectionFinder.Mode.DRIVING);
  directionFinder.setOrigin(origin);
  directionFinder.setDestination(destination);

I run the function and sometimes work, and sometimes not. Which annoys me when I go to my google-console-dev, in my admin administration I don't see any movement. As if nothing had happened even if I turned on the API in the Google console.

I know exactly the same as in these links:

Maps.newDirectionFinder()

, API , .

API- script

, API.

, Maps, URLfetch?. , , , .

, Maps , :

Maps.setAuthentication(clientId, signedKey)

projectId Api-key, , .

, , , - !

!

, , , ;)

+4
1

API, . , . , , - , , , , , , , .

.

function Maps1()
{
   var s = '',t = '';
   var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Maps1');
   var rng = sht.getDataRange();
   var rngA = rng.getValues();
   var origin = rngA[1][0];
   var destination = rngA[rngA.length-1][0];
   var waypoints = 0;
   var points = [];
   var r1 = new RegExp('<b>', 'g');
   var r2 = new RegExp('</b>', 'g');
   var r3 = new RegExp('<div style="font-size:0.9em">', 'g');
   var r4 = new RegExp('</div>', 'g');
   var currentLabel = 0;
   var directions = Maps.newDirectionFinder();
   var map = Maps.newStaticMap().setSize(600,400);
   directions.setOrigin(origin);
   for(var i=2;i<rngA.length-1;i++)
   {
     directions.addWaypoint(rngA[i][0]);
     waypoints++;
   }
   directions.setDestination(destination);
   directions.setMode(Maps.DirectionFinder.Mode.DRIVING);
   var cnt = 1;
   rngA[cnt][1]= 0;
   rngA[cnt][2]= 0;
   var results = directions.getDirections();
   for (var i in results.routes) {
    for (var j in results.routes[i].legs) {
      rngA[++cnt][1]= Number(results.routes[i].legs[j].distance.value) * 0.000621371;//this converts from meters to miles
      rngA[cnt][2]= results.routes[i].legs[j].duration.text;
      for (var k in results.routes[i].legs[j].steps) {
        var step = results.routes[i].legs[j].steps[k];
        var path = Maps.decodePolyline(step.polyline.points);
        var text = step.html_instructions;
        points = points.concat(path);
        text = text.replace(r1, ' ');
        text = text.replace(r2, ' ');
        text = text.replace(r3, ' ');
        text = text.replace(r4, ' ');
        t += '<br />' + ++currentLabel + ' - ' + text + '<span style="color:red;">Distance: </span>' + step.distance.text + '<span style="color:red;"> Duration: </span>' + step.duration.text ;
      }
    }
  }
  var lpoints=[];
  if (points.length < 200)
  {
    lpoints = points;
  }
  else 
  {
    var pCount = (points.length / 2);
    var step = parseInt(pCount / 100);
    for (var i = 0; i < 100; ++i) 
    {
      lpoints.push(points[i * step * 2]);
      lpoints.push(points[(i * step * 2) + 1]);
    }
  }
  if (lpoints.length > 0) 
  {
    var pline = Maps.encodePolyline(lpoints);
    map.addPath(pline);
  }

   s += '<img src="' + map.getMapUrl() + '" /><br />';
   s += t;
   rng.setValues(rngA);
   SpreadsheetApp.flush();
   var totalDistance = 0;
   for(var i=2;i<rngA.length;i++)
   {
     totalDistance += Number(rngA[i][1]);
   }
   s += '<br /> Total Distance: ' + totalDistance + ' Miles';
   dispStatus('Route Data', s, 1200, 500);
 }

.

function dispStatus(title,html,width,height,modal)
{
  var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
  var width = typeof(width) !== 'undefined' ? width : 400;
  var height = typeof(height) !== 'undefined' ? height : 300;
  var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
  var modal = typeof(modal) !== 'undefined' ? modal : false;
  var htmlOutput = HtmlService
     .createHtmlOutput(html)
     .setWidth(width)
     .setHeight(height);
 if(!modal)
 {
   SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
 }
 else
 {
   SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
 }
} 

:

enter image description here

, , Route 1, , , . script , .

, .

enter image description here

-1

Source: https://habr.com/ru/post/1676899/


All Articles