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;
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);
}
}
:

, , Route 1, , , . script , .
, .
