我在谷歌表格中使用這個腳本來計算我的里程費用。
/**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/
function GOOGLEMAPS(start_address,end_address,return_type) {
// https://www.chicagocomputerclasses.com/
// Nov 2017
// improvements needed
var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();
var getTheLeg = directions["routes"][0]["legs"][0];
var meters = getTheLeg["distance"]["value"];
switch(return_type){
case "miles":
return meters * 0.000621371;
break;
case "minutes":
// get duration in seconds
var duration = getTheLeg["duration"]["value"];
//convert to minutes and return
return duration / 60;
break;
case "hours":
// get duration in seconds
var duration = getTheLeg["duration"]["value"];
//convert to hours and return
return duration / 60 / 60;
break;
case "kilometers":
return meters / 1000;
break;
default:
return "Error: Wrong Unit Type";
}
}
這作業正常,但我希望腳本選擇距離最短的路線而不是時間,就像在這個例子中一樣:


它給了我 22.7 公里,但我希望每次都能給我最短的距離,在這種情況下為 21.3 公里
我可以在腳本中更改哪些內容,以便它采用距離最短的路線而不是時間最短的路線?謝謝!
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想檢索最小距離。
- 您想使用 Google Apps 腳本的自定義函式來實作這一點。
在這種情況下,為了檢索多條路線,我使用了setAlternatives(useAlternatives). 并且,從檢索到的多條路線中檢索最小距離。
當這反映在你的腳本中時,它變成如下。請按如下方式修改您的腳本。
從:
var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();
var getTheLeg = directions["routes"][0]["legs"][0];
var meters = getTheLeg["distance"]["value"];
到:
var mapObj = Maps.newDirectionFinder().setAlternatives(true);
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();
var getTheLeg = directions["routes"][0]["legs"][0];
var meters = Math.min(...directions.routes.map(({legs: [{distance: {value}}]}) => value));
如果你想使用的值
getTheLeg從相同routes的meters,請修改如下。從
var mapObj = Maps.newDirectionFinder(); mapObj.setOrigin(start_address); mapObj.setDestination(end_address); var directions = mapObj.getDirections(); var getTheLeg = directions["routes"][0]["legs"][0]; var meters = getTheLeg["distance"]["value"];到
var mapObj = Maps.newDirectionFinder().setAlternatives(true); mapObj.setOrigin(start_address); mapObj.setDestination(end_address); var directions = mapObj.getDirections(); var {getTheLeg, meters} = directions.routes.map(({legs: [{duration, distance}]}) => ({duration, distance})).reduce((o, {duration, distance}, i) => { if (i == 0 || o.meters > distance.value) { o.meters = distance.value; o.getTheLeg.duration.value = duration.value; } return o; }, {getTheLeg: {duration: {value: 0}}, meters: 0});
筆記:
- 在這種情況下,如果您沒有啟用 Google Apps Script 專案的 V8 運行時,請啟用它。
參考:
- setAlternatives(useAlternatives)
- 地圖()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348717.html
